WordPress.org

Ready to get started?Download WordPress

Forums

Paging not working for custom loop (2 posts)

  1. Cedric_89
    Member
    Posted 1 year ago #

    I've gone through several examples on how to do paging for a custom loop, yet I can't get the next/previous links to show up with the following code in archive-event.php.
    (The plug-in used is Event Organiser, so I suppose the post_type is registered properly.)

    <?php
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$temp = $wp_query;
    	$wp_query= null;
    
    	$wp_query = new WP_Query( array( 'post_type' =>'event', 'posts_per_page' => 1,  'paged' => $paged ) );
    ?>
    
    <?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    <!-- The Loop -->
    <?php endwhile; endif; ?>
    
    <?php
        	$wp_query = null;
    	$wp_query = $temp;
    	wp_reset_query();
    ?>

    Next/previous links in header.php:

    <div class="next-post-link paging svg">
    	<?php next_posts_link('', '0') ?>
    </div>
    <div class="previous-post-link paging svg">
    	<?php previous_posts_link('', '0') ?>
    </div>

    This here is the page with the code in question: http://allesaffen.sg/?post_type=event

    There are two event posts, with paging set to 1, the next post link should actually show up but doesn't. When I enter -1 all event posts are showing up as expected.

  2. vtxyzzy
    Member
    Posted 1 year ago #

    AFAIK, previous/next_posts_link() only works with query_posts(), not with WP_Query. Try saving the current query and using query_posts() instead of WP_Query().

Topic Closed

This topic has been closed to new replies.

About this Topic