Community Forums › Forums › Archived Forums › Design Tips and Tricks › adding pagination to custom post type page template
Tagged: post navigation
- This topic has 6 replies, 3 voices, and was last updated 8 years, 1 month ago by Brad Dalton.
-
AuthorPosts
-
February 19, 2016 at 7:48 pm #179435dmcleod87Member
hi all,
working on a new portfolio website and have setup a CPT named 'portfolio'. I have also created a custom page template for each portfolio item. what i want to do is to add pagination to the bottom of this page to in order to cycle through the other portfolio items. below is the code i've used for my custom page template. the theme is based on the Genesis Sample theme.
can someone point me in the right direction? i've tried a few solutions that i found online but nothing seemed to work...thanks!
<?php /* Template Name: Portfolio Item */ remove_action('genesis_loop', 'genesis_do_loop'); add_action('genesis_loop', 'custom_loop'); function custom_loop() { global $paged; $args = array('post_type' => 'portfolio'); ?> <article itemtype="http://schema.org/CreativeWork" itemscope="itemscope" class="post-<?php print $pageid; ?> page type-page status-publish entry"> <div class="entry-content" itemprop="text"> <div class="custom-page-content"> <?php if(have_posts()) : while(have_posts()) : the_post(); echo '<div class="intro-text-wrapper">'; /* Display the page title */ echo '<h1 class="page-title"> ' . get_field('project_page_title') . ' </h1>'; /* The following code displays the Advanced Custom Fields meta boxes */ echo '<div class="intro-text"> ' . get_field('project_intro_text') . ' </div>'; echo '</div>'; /* Show page content below intro text */ do_action( 'genesis_entry_content' ); endwhile; endif; ?> </div></div></article> <?php } genesis();
February 20, 2016 at 12:57 am #179443Brad DaltonParticipantIs this an archive page or a single-portfolio.php?
For single posts/cpt you would use previous_post_link and next_post_link for post navigation
For archives you would use the_posts_pagination for Previous/next page navigation
February 20, 2016 at 2:05 am #179451dmcleod87Memberhi brad,
it's a single-portfolio.php template. is this snippet what i would be looking for? https://wpbeaches.com/add-custom-post-type-navigation-links-in-genesis/
i tried it just then and no luck.
February 25, 2016 at 5:00 pm #179925dmcleod87Memberfigured it out. using the tutorial i mentioned above and following brad's advice, i added parts of the snippet to my custom template, rather than to functions.php
hope this helps someone else in the future!
<?php /* Template Name: Portfolio Item */ remove_action('genesis_loop', 'genesis_do_loop'); add_action('genesis_loop', 'custom_loop'); function custom_loop() { global $paged; $args = array('post_type' => 'portfolio'); genesis_markup( array( 'html5' => '<div %s>', 'xhtml' => '<div class="navigation">', 'context' => 'adjacent-entry-pagination', ) ) ?> <article itemtype="http://schema.org/CreativeWork" itemscope="itemscope" class="post-<?php print $pageid; ?> page type-page status-publish entry"> <div class="entry-content" itemprop="text"> <div class="custom-page-content"> <?php if(have_posts()) : while(have_posts()) : the_post(); echo '<div class="intro-text-wrapper">'; /* Display the page title */ echo '<h1 class="page-title"> ' . get_field('project_page_title') . ' </h1>'; /* The following code displays the Advanced Custom Fields meta boxes */ echo '<div class="intro-text"> ' . get_field('project_intro_text') . ' </div>'; echo '</div>'; /* Show page content below intro text */ do_action( 'genesis_entry_content' ); // Add pagination Portfolio Post Type echo '<div class="pagination-previous alignleft">'; previous_post_link(); echo '</div>'; echo '<div class="pagination-next alignright">'; next_post_link(); echo '</div>'; echo '</div>'; endwhile; endif; ?> </div></div></article> <?php } genesis();
February 26, 2016 at 2:08 am #179964Brad DaltonParticipantGood stuff. Genesis do have a function for single post navigation however it includes the conditional
is_singular('post')
If they changed that to
is_singular()
it could also be used on CPT's
/** * Display links to previous and next post, from a single post. * * @since 1.5.1 * * @return null Return early if not a post. */ function genesis_prev_next_post_nav() { if ( ! is_singular() ) return; genesis_markup( array( 'html5' => '<div %s>', 'xhtml' => '<div class="navigation">', 'context' => 'adjacent-entry-pagination', ) ); echo '<div class="pagination-previous alignleft">'; previous_post_link(); echo '</div>'; echo '<div class="pagination-next alignright">'; next_post_link(); echo '</div>'; echo '</div>'; }
February 27, 2016 at 9:17 am #180060himanshMemberHi, i tried to add post navigation to single posts, using the snippet provided here : http://my.studiopress.com/snippets/entry-content/
but its just showing link to the post - "Hello World" only after entry.
Any Hint why its behaving like this ?
Thanks in advance
February 27, 2016 at 11:01 am #180071Brad DaltonParticipantMust be the first post. Check the 2nd post which should includes links to both the previous and next post.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.