自定义帖子类型分页不起作用

时间:2016-08-05 07:12:53

标签: php wordpress pagination custom-post-type

我正在创建自定义主题,也有自定义帖子类型。 但是,自定义帖子类型的分页无效。我尝试了Stack Overflow的所有可能的解决方案,但都是徒劳的。

以下是代码:

<?php    global $wp_query;                  
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
        $args = array( 
            'posts_per_page' => 3, 
            'post_type' => 'services',
            'orderby' => 'date', 
            'order' => 'DESC',
            'nopaging' => false,
            'paged'=>$paged
        );
    $the_query = new WP_Query( $args ); ?>


<div class="service-content clearfix">
    <ul class="clearfix">
    <?php if ( $the_query->have_posts() ) :  while ($the_query->have_posts()) : $the_query->the_post(); ?>
        <?php $word_count = strlen( wp_strip_all_tags($post->post_content)); 
            $id = get_the_ID();?>                   
        <li class="col-sm-4 wow fadeInDown animated" data-wow-delay="300ms" data-wow-duration="500ms">
            <figure class="image">
                <?php the_post_thumbnail( 'medium' ); ?>
            </figure>
            <?php if($word_count<269){ ?>
                <h3><?php echo $post->post_title; ?></h3>
                <p><?php echo $post->post_content; ?></p>
            <?php } else{ ?>
            <h3><?php echo $post->post_title; ?></h3>
                <?php echo $post->post_content; ?>
                <?php } ?>                                                  
        </li>
        <?php endwhile;
            next_posts_link();
            previous_posts_link();?>
        <?php wp_reset_query(); ?>

<?php endif; ?>
    </ul>
</div>

在这里,posts_per_page正在运作但Pagination无效,有什么帮助?

3 个答案:

答案 0 :(得分:1)

请根据需要使用以下代码..

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$next = $paged+1;
$prev= $paged-1;
<a href="<?php echo '/page/'.$next; ?>" >NEXT</a>
<a href="<?php echo '/page/'.$prev; ?>" >PREV</a>

答案 1 :(得分:1)

只需使用paginate_links功能

即可

根据您的查询和分页显示分页

$paginate_args = array(
    'base'               => '%_%',
    'format'             => '?paged=%#%',
    'total'              => $the_query ->max_num_pages, 
    'current'            => $paged, 
    'prev_text'          => __('«'),
    'next_text'          => __('»'),
);

echo paginate_links( $paginate_args );

确保baseformat正确无误。基于您的永久链接结构

答案 2 :(得分:1)

Wordpress中的分页问题之一是,每页的帖子数值被忽略,Wordpress设置的阅读页面中的博客页面最多显示设置被视为实际值。所以,你试图在每页和第一页上显示3个有效。但是,当您转到第二页时,Wordpress正在加载不同的偏移量,因此您的代码将无法正常工作。

在Wordpress Stack Exchange上已经解释了这个问题,所以我不会重复该用户的回答。你可以在这里阅读更多内容:

https://wordpress.stackexchange.com/questions/30757/change-posts-per-page-count

那应该为你解决问题。

相关问题