发布页面分页不起作用-Wordpress WP-PageNavi

时间:2018-06-20 07:07:20

标签: php wordpress pagination

我在分页的wordpress发布页面上遇到麻烦。我已经安装了分页插件(WP-PageNavi),到目前为止,我已经可以打印我的所有帖子,但是分页仅显示一页(当前一页)。如何使分页正常工作?

<?php
// args query
$args = array(
    'post_type'      => 'post',
    'order'          => 'DESC'
);

// custom query
$recent_posts = new WP_Query($args);

// check that we have results
if($recent_posts->have_posts()) : ?>

<ul class="article_list">

    <?php
    // start loop
    while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>

    <li class="regular">
        <a href="<?php echo get_permalink(); ?>">
            <div class="text">
                <p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
                <h3 class="article_title"><?php echo mb_strimwidth(get_the_title(), 0, 80, '...'); ?></h3>
                <p class="date"><?php echo get_the_date( 'Y-m-d' ); ?></p>
            </div>
            <div class="mask">
                <img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
            </div>
        </a>
    </li>
<?php endwhile; ?>
</ul>
<?php endif;
// reset query
wp_reset_postdata();
?>

<?php include($path.'libs/pagination.php'); ?>

这是我的pagination.php文件:

<div class="pagination">
  <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
</div>

2 个答案:

答案 0 :(得分:1)

我想您已经放了wp_pagenavi();可以使用libs / pagination.php。

因此,将其替换为

wp_pagenavi(array( 'query' => $recent_posts )); 

因此,最终代码应该看起来像

<?php
// args query
$args = array(
    'post_type'      => 'post',
    'order'          => 'DESC'
);

// custom query
$recent_posts = new WP_Query($args);

// check that we have results
if($recent_posts->have_posts()) : ?>

<ul class="article_list">

    <?php
    // start loop
    while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>

    <li class="regular">
        <a href="<?php echo get_permalink(); ?>">
            <div class="text">
                <p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
                <h3 class="article_title"><?php echo mb_strimwidth(get_the_title(), 0, 80, '...'); ?></h3>
                <p class="date"><?php echo get_the_date( 'Y-m-d' ); ?></p>
            </div>
            <div class="mask">
                <img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
            </div>
        </a>
    </li>
<?php endwhile; ?>
</ul>
<?php endif;
echo '<div class="pagination">';
if(function_exists('wp_pagenavi')) { wp_pagenavi(array( 'query' => $recent_posts )); }
echo '</div>';
wp_reset_postdata();
?>

答案 1 :(得分:0)

我在代码中发现了问题: 我没有在查询中添加任何用于分页的内容,因此它返回了我的所有帖子。 因此,我在自定义查询中添加了分页逻辑,它确实起作用。

<?php   $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $custom_args = array(
    'post_type' => 'post',
    'order' => 'DESC',
    'posts_per_page' => 5,
        'paged' => $paged
    );

// custom query
$recent_posts = new WP_Query($custom_args);

// check that we have results
if($recent_posts->have_posts()) : ?>

<ul class="article_list">

    <?php
    // start loop
    while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>

    <li class="regular">
        <a href="<?php echo get_permalink(); ?>">
            <div class="text">
                <p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
                <h3 class="article_title"><?php echo mb_strimwidth(get_the_title(), 0, 80, '...'); ?></h3>
                <p class="date"><?php echo get_the_date( 'Y-m-d' ); ?></p>
            </div>
            <div class="mask">
                <img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
            </div>
        </a>
    </li>
<?php endwhile; ?>
</ul>
<?php endif;
echo '<div class="pagination">';
if(function_exists('wp_pagenavi')) { wp_pagenavi(array( 'query' => $recent_posts )); }
echo '</div>';
wp_reset_postdata();
?>