Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDavid Shen
13,611 PointsHow to add navigation for next/previous page of posts?
I set show 5 posts in each page from the settings and I try to add a navigation for linking to the next 5 posts with the same type. Try couple times and methods but still couldn't figure it out.
Here is my code, please advise
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'custom', 'paged' => $paged);
$my_query = new WP_Query($args);
$taxonomies = get_taxonomies();
if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?>
<h4><?php echo get_the_date('F j Y'); ?></h4>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<p><?php the_field('book'); ?> <?php the_field('start'); ?> - <?php the_field('end'); ?></p>
<?php $content = get_the_content(); ?>
<?php echo do_shortcode( '[audio]' ); ?>
<span> by <?php echo get_the_term_list( $post->ID, 'author'); ?></span>
<p>Tags: <?php the_field('language'); ?> | <?php the_field('section'); ?></p>
<hr>
<?php endwhile; ?>
<?php previous_posts_link( 'Prev' ) ?> |
<?php next_posts_link( 'Next', 10 ); ?>
<?php } ?>
<?php wp_reset_postdata(); ?>
David Shen
13,611 PointsI put the semicolon but it's still doesn't work. The url slug does change tho
mydomain.com/custom/page/1/
mydomain.com/custom/page/2/
mydomain.com/custom/page/3/
...
1 Answer
Nathan Newell
7,634 PointsIn the case that a static page is being set as the home page. use get_query_var('page') instead of get_query_var('paged').
Also if you have any custom queries before this, put wp_reset_query(); before using get_query_var('page') or use get_query_var('paged')
David Shen
13,611 PointsI try to use 'page' but it still not working...
Roberto Alicata
Courses Plus Student 39,959 PointsRoberto Alicata
Courses Plus Student 39,959 Pointsa semicolon is missing in:
php previous_posts_link( 'Prev' )