0

I'm working on a WP_Query using the Modern Tribe Events Calendar

My problem is with pagination.

Is this the incorrect way to paginate the WP_Query or could there be another method for paginating the Events Calendar?

I found a tutorial that uses the WPNavi plugin but I would like use core wordpress functions.

<?php 
$upcoming = new WP_Query();
$upcoming->query( array(
    'post_type'=> 'tribe_events',
    'eventDisplay' => 'upcoming',
    'posts_per_page' => 1)
);

if ($upcoming->have_posts()) : while ($upcoming->have_posts()) : $upcoming->the_post(); ?>

        <?php tribe_get_template_part( 'list/single', 'event' ) ?>

<?php endwhile;

else : 
    echo 'no upcoming events'; 
endif;

wp_reset_query(); 
?>

<div class="navigation"><p><?php posts_nav_link(); ?></p></div>
1
  • Please do not add the answer to the question, use the answer field instead.
    – fuxia
    Commented Jul 11, 2013 at 18:23

2 Answers 2

0

posts_nav_link() works off the main query. Instead of invoking your own $upcoming, just use query_posts( ... ) to temporarily overwrite it, and the corresponding template tags...

query_posts(
    array(
        'post_type'      => 'tribe_events',
        'eventDisplay'   => 'upcoming',
        'posts_per_page' => 1,
    )
);

if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <?php tribe_get_template_part( 'list/single', 'event' ) ?>

<?php endwhile;

else : 
    echo 'no upcoming events'; 
endif;

wp_reset_query();
2
  • Thank you for your response. When I try to include posts_nav_link(); I get the page links and it pages correctly according to the number of posts but it only shows the first post on each page.
    – hyperdrive
    Commented Jul 12, 2013 at 15:50
  • That's because you have posts_per_page => 1... Commented Jul 13, 2013 at 1:58
0

I was able to get this to work using this solution.

My only concern is if its an appropriate solution.

 <?php 
    global $paged;
    $curpage = $paged ? $paged : 1;
    $upcoming = new WP_Query();
    $upcoming->query( array(
        'post_type'=> 'tribe_events',
        'eventDisplay' => 'upcoming',
        'posts_per_page' => 2,
        'paged' => $paged)
    );

    if ($upcoming->have_posts()) : while ($upcoming->have_posts()) : $upcoming->the_post(); ?>

        <?php tribe_get_template_part( 'list/single', 'event' ) ?>

    <?php endwhile;


    echo '<div id="navigation">';
        if ($curpage != 1) {
           echo '<a class="previous" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">&lsaquo;</a>';
        }
        if ($curpage != $upcoming->max_num_pages) {
           echo '<a class="next" href="'.get_pagenum_link(($curpage+1 <= $upcoming->max_num_pages ? $curpage+1 : $upcoming->max_num_pages)).'">&rsaquo;</a>';
        }            
    echo '</div>';

    endif;

    wp_reset_query(); 
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.