I have a site on which a lot of video's will be posted next week. The videos are posted with a custom post type, with just 'to', 'from' and yt video id. I wan to load a lot the videos on an archieve page, but not more than x per page. So x posts on page 1, x on page 2 and so on. Unfortunately, the page number doesn't show up, the 'p' container just stays empty. Here is my code:
$args = array( 'post_type' => 'video'/*, 'posts_per_page' => 3 */);
$loop = new WP_Query( $args );
if (have_posts()) :
while ( $loop->have_posts()) : $loop->the_post();
<?php endwhile; ?>
<p><?php posts_nav_link('∞','«« View newer posts','View older posts »»'); ?></p>
<?php endif; ?>
I have emptied the part between the while, because in that part I only call to basic functions like the_permalink and the_content.
As it turns out, using posts_nav_link in tandem with WP_Query doesn't work as expected, since posts_nav_link relies on the global $wp_query object, and doesn't recognize the custom WP_Query instance.
This Answer should provide you the details you need in order to get everything to work.
<?php
global $wp_query;
$temp_wp_query = $wp_query;
$wp_query = null;
$paged = get_query_var('paged');
$args = array(
'post_type' => 'video',
'paged' => $paged ? $paged : 1
/*, 'posts_per_page' => 3 */
);
$loop = new WP_Query( $args );
$wp_query = $loop;
if ($loop->have_posts()) :
while ( $loop->have_posts()) : $loop->the_post();
<?php endwhile; ?>
<p><?php posts_nav_link('∞','«« View newer posts','View older posts »»'); ?></p>
<?php
endif;
$wp_query = $temp_wp_query;
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.