Community Forums › Forums › Archived Forums › General Discussion › Custom Fields, CPT UI, and a custom page template
Tagged: archive template, CPT
- This topic has 6 replies, 2 voices, and was last updated 6 years ago by
RickStewart.
-
AuthorPosts
-
March 11, 2019 at 11:27 am #489940
RickStewart
ParticipantHello,
Step 1 - I have set up a a few custom fields in a new field group called Book Metadata. I have a rule to add this group in when the post type == books.
Step 2 - I have created a Custom Post Type using CPT UI called books. So far so good...
Step 3 - I wanted Books to have its own archive page with a different layout so I created a custom page template called archive-books.php set with /* Template Name: Archive Book Template */. This template is being used by Books without any other changes.
Again, so far so good.
So now blogs use https://localhost:8080/wordpress/layouts/full-width-content/ and Books uses https://localhost:8080/wordpress/books/
This is as it should be operational wise, and I would have moved unto something else except for one issue, pagination does not work, I get 404 errors. This turns out to be common when using a custom blog template, but after testing many fixes over maybe 10 hours I decided it was time to stop, take a step back, and examine the basics.
So finally, here is my question I am hoping someone here can answer.
In step 3 above, I have assumed because it was working that I have done this correctly. But have I? Is all that is needed for a CPT to use a custom template is to simply name a new file archive-{CPT-name}.php, place it in the child theme root, and customize the genesis loop? Have I missed a step or something?
If I can be sure I did not screw this part up and I continue to troubleshoot the pagination issue until I solve it.
Thanks so much!
Rick
Warm regards,
Rick Stewart
web: https://windyhilldevelopers.com
email: [email protected]March 11, 2019 at 12:30 pm #489947Brad Dalton
ParticipantMarch 11, 2019 at 2:40 pm #489954RickStewart
ParticipantHi Brad,
Its on a local XAMPP instance.
I'll copy and paste the template page here in hopes that might be of help, but I can push the whole thing out to a DO droplet if necessary.
Thanks much for taking a look!
Rick
/* Template Name: Archive Book Template */ //* Remove the breadcrumb navigation remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' ); //* Remove the post info function remove_action( 'genesis_entry_header', 'genesis_post_info' ); //* Remove the post content remove_action( 'genesis_entry_content', 'genesis_do_post_content' ); //* Remove the post image remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 ); //* Add books-archive-page body class to the head add_filter( 'body_class', 'add_books_archive_page' ); function add_books_archive_page( $classes ) { $classes[] = 'books-archive-page'; return $classes; } //* Display Category Description add_action( 'genesis_before_loop', 'display_category_archives_description'); function display_category_archives_description () { echo category_description( '$category_id' ); } //* Remove the post meta function remove_action( 'genesis_entry_footer', 'genesis_post_meta' ); // remove Leave a Comment /*************************************************************************************/ add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' ); /* Custom Loop */ function books_custom_loop() { global $post; global $paged; global $wp_query; $custom_query_args = array( 'post_type' => '', 'posts_per_page' => get_option( 'posts_per_page' ), 'post_status' => 'publish', 'paged' => 'paged', 'nopaging' => false, 'ignore_sticky_posts' => true, //'category_name' => 'custom-cat', 'order' => 'DESC', // 'ASC' 'orderby' => 'date' // modified | title | name | ID | rand ); // Pagination fix https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops $custom_query_args['paged'] = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1; $custom_loop = new WP_Query( $custom_query_args ); $temp_query = $wp_query; $wp_query = NULL; $wp_query = $custom_loop; if ( $custom_loop->have_posts() ): while ( $custom_loop->have_posts() ): $custom_loop->the_post(); global $post; $custom_title = get_the_title(); $custom_entry = get_the_content(); echo "<div class='custom_block'>"; echo "<div class='custom_title'>$custom_title</div>"; echo "<div class='custom_entry'>$custom_entry</div>"; echo "</div>"; endwhile; endif; // Reset postdata wp_reset_postdata(); // Custom query loop pagination previous_posts_link( 'Previous Books' ); next_posts_link( 'Next Books', $custom_loop->max_num_pages ); // Restore original main query object $wp_query = NULL; $wp_query = $temp_query; } add_action( 'genesis_loop', 'books_custom_loop' ); remove_action( 'genesis_loop', 'genesis_do_loop' ); genesis();
Warm regards,
Rick Stewart
web: https://windyhilldevelopers.com
email: [email protected]March 11, 2019 at 3:15 pm #489955Brad Dalton
ParticipantWhy do you need a custom loop?
I would use a simple template like this and name it archive-books.php
March 11, 2019 at 4:00 pm #489956RickStewart
ParticipantHey Brad,
Well 2 reasons I suppose.
First this is strictly self training, I want to understand how this is suppose to work.
And two, if I understand the solution you linked, it only adds the new post type "games" to the current genesis loop, it does not allow you to customize the loop output, is that correct? What I am after is two different layouts, one layout for all the usual blog posts and a totally different layout for the "books" custom posts, and I think that would not be the case with your example, or am I missing something? ( could be! I often do... lol... )
But really I was just looking for someone here to confirm one way or the other if creating a new archive template page by simply creating a file called archive-books.php and then give it a template name /* Template Name: Archive Book Template */ was all that I needed to do to get my custom post type "book" to display in its own loop on its own page?
Cheers,
Rick
Warm regards,
Rick Stewart
web: https://windyhilldevelopers.com
email: [email protected]March 11, 2019 at 4:22 pm #489957Brad Dalton
ParticipantUse the WordPress Template Hierarchy to name your files.
single-books.php
and
archive-books.php
No need to add a template header.
No need to add code for a new WP_Query to create a custom loop.
March 11, 2019 at 4:43 pm #489959RickStewart
ParticipantSuper! Thanks a lot Brad - that was what I wanted to confirm.
Now back to trying to solve the pagination problem.
Rick
Warm regards,
Rick Stewart
web: https://windyhilldevelopers.com
email: [email protected] -
AuthorPosts
- The topic ‘Custom Fields, CPT UI, and a custom page template’ is closed to new replies.