zoukankan      html  css  js  c++  java
  • Wordpress 作者模板页中的自定义帖子类型分页问题

    <?php 
                        // 获取当前页面的页数,函数的参数为 paged
                        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                        $args = array( 
                            'post_type' => 'knowledgebase', 
                            'post_status' => 'publish',
                            'author'    =>  get_queried_object_id(),
                            'posts_per_page' => 10,
                            'paged'=> $paged,
                        );
                        $the_query = new WP_Query( $args ); // 注意这里需要区别 global $wp_query
                        while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
    
                            <?php get_template_part( 'content-author', 'knowledgebase' ); ?>
                        <?php endwhile;  ?>
                <!-- 分页 -->
                        <nav class="navigation paging-navigation" role="navigation">
                            <div class="nav-links">
                                <?php if ( get_previous_posts_link() ) : ?>
                                    <p class="fleft"><?php previous_posts_link( __( '<span class="meta-nav">&laquo;</span>', 'drivereasy' ) ); ?></p>
                                <?php endif; 
                                
                                if ( $the_query->max_num_pages > 1 ) : ?>
                                    <p class="fleft"><?php printf( __('Page %s of %s', 'drivereasy'), $paged, $the_query->max_num_pages ); ?></p>
                                <?php endif; ?>
    
                                <?php if ( get_next_posts_link() ) : ?>
                                    <p class="fleft"><?php next_posts_link( __( '<span class="meta-nav">&raquo;</span>', 'drivereasy' ) ); ?></p>
                                <?php endif; ?>
                            </div><!-- .nav-links -->
                        </nav><!-- .navigation -->

    以上分页默认是根据 post 文章的数量进行分页,所以以上分页不准确,有些页面会打不开,必须在 function.php 加上以下代码才能正常分页

    function author_custom_post($query) {
            if ( !is_admin() && $query->is_author() && $query->is_main_query() ) :
                $query->set('post_type', array('knowledgebase'));
    $query->set('post_per_page', 10);  // 每页显示的文章数量必须与author.php 中的保持一致
               endif;
            }
    add_action('pre_get_posts', 'author_custom_post');

    资料来源:https://forums.envato.com/t/custom-post-type-pagination-issue-in-author-template/106090/2

  • 相关阅读:
    windows和linux下安装 redis
    YII1 配置redis并使用
    YII1 安装smarty模版引擎
    thinkphp5 阿里云短信 发送多参数的短信
    利用securecrt或者xshell 下载服务器端文件到本地windows系统中
    Swift 内存管理
    扩展和协议
    继承
    构造与析构
    方法
  • 原文地址:https://www.cnblogs.com/ryanzheng/p/10406975.html
Copyright © 2011-2022 走看看