简体   繁体   English

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

[英]Custom post type pagination is not working

I am creating my custom theme and there's Custom Post Type too. 我正在创建我的自定义主题,也有“自定义帖子类型”。 However, pagination for the Custom Post Type is not working. 但是, 自定义帖子类型的分页不起作用。 I tried all the possible solution from Stack Overflow but all in vain. 我尝试了Stack Overflow的所有可能解决方案,但徒劳无功。

Here's the code: 这是代码:

<?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>

Here, posts_per_page is working but Pagination not working , any help? 在这里, posts_per_page在工作,但是Pagination不工作,有什么帮助吗?

Please use bellow codes as your need.. 请根据需要使用波纹管代码。

$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>

Just use paginate_links function 只需使用paginate_links函数

to display the pagination based on your query and paging 显示基于您的查询和分页的分页

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

echo paginate_links( $paginate_args );

Make sure the base and format are correct. 确保baseformat正确。 based on your permalink structure 根据您的永久链接结构

One of the problems with pagination in Wordpress is that the Posts per page value is ignored and the Blog pages show at most setting in the Reading page of the Wordpress settings is taken as the actual value. Wordpress中分页的问题之一是每页的帖子数被忽略,而在Wordpress的“阅读”页中最多显示博客”页面设置为实际值。 So, you are trying to show 3 per page and on the first page that works. 因此,您尝试在每页上显示3并在第一页上显示。 However, when you go to the second page, Wordpress is loading a different offset so your code will not work. 但是,当您转到第二页时,Wordpress将加载其他偏移量,因此您的代码将无法工作。

This issue has been explained extremely well over on the Wordpress Stack Exchange so I won't repeat that user's answer. 这个问题已经在Wordpress Stack Exchange上得到了很好的解释,所以我不再重复该用户的回答。 You can read more here: 你可以在这里阅读更多:

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

That should sort out the problem for you. 那应该为您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM