0

我遇到了 Modern Tribe 的“事件日历”插件无法正确分页的问题。它可以很好地显示第一页,但拒绝转到任何辅助页面(使用 wp-pagenavi)。理想情况下,我试图通过 AJAX 加载它,但此时分页将是一个奇迹。我在这里已经筋疲力尽了一天。基本上,这是查询:

<?php 
$wp_query = new WP_Query();
$wp_query->query( array(
'post_type'=> 'tribe_events',
'eventDisplay' => 'upcoming',
'posts_per_page' => 5)
);
$max = $wp_query->max_num_pages;
$paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;


if ($wp_query->have_posts()) :
while ($wp_query->have_posts()) :
    $wp_query->the_post();
    ?>
  <li><!-- do stuff    -->  </li>
  <?php
endwhile;?>
    </ul>
<?php
endif;
wp_reset_query(); // important to reset the query
?>
4

1 回答 1

1

There are quite a number of issues with your loop:

You say you're using wp_page_navi, however I'm not seeing any references to the plugin in any of your code. Also, you have list items being generated in your loop along with a closing ul tag, but I don't see any opening ul tag anywhere, which could also be contributing to some of your problems.

I'm also noticing in your list of arguments that you're trying to set 'eventDisplay' to 'upcoming'. 'eventDisplay' is not a valid WP_Query parameter. I'm guessing you probably have a registered Taxonomy of eventDisplay? If so, you will need to use Tax Query instead. I've removed this parameter in the example, but feel free to replace it when you're comfortable with setting the parameters you need.

Lastly, query arguments should be made when you call WP_Query, not through $query->query.

Here's something I came up with using standard Wordpress Paging and the arguments you have in your code. I'm not familiar with wp_page_navi, but this should help get you started on the right track:

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

if ($query->have_posts()) :
?>
<ul>
<?php while ($query->have_posts()) : $query->the_post(); ?>
    <li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
echo '<div id="wp_pagination">';
    echo '<a class="first page button" href="'.get_pagenum_link(1).'">&laquo;</a>';
    echo '<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">&lsaquo;</a>';
    for($i=1;$i<=$query->max_num_pages;$i++)
    {
        echo '<a class="'.($active = $i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
    }
    echo '<a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">&rsaquo;</a>';
    echo '<a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">&raquo;</a>';
echo '</div>';
endif;
wp_reset_query();
?>

This will set your loop to display 5 post titles in your list. Below it will be a series of numbered links based on how many posts you have. When you click on a number, it reloads the page with its corresponding titles.

Let me know if this helps.

于 2012-05-15T01:52:20.570 回答