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

时间:2017-03-03 作者:Sarjil

我想在自定义帖子类型的单个页面中添加分页。

这是单页(自定义帖子类型)的代码:

<?php
    $paged = get_query_var(\'paged\') ? get_query_var(\'paged\') : 1;
    $args = array(\'post_type\' => \'news\', \'posts_per_page\' => 1, \'paged\' => $paged);
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
?>

// Loop

<?php endwhile; wp_reset_postdata(); ?>

<div id="pagination" class="clearfix">
    <?php next_posts_link( \'Older Entries\', $loop->max_num_pages );
previous_posts_link( \'Newer Entries\' );//posts_nav_link(); ?>
</div>

////

Function page code ////


////

add_action( \'init\', \'create_post_type\' );
function create_post_type() {
    register_post_type( \'news\',
        array(\'labels\' => array(
            \'name\' => __(\'News\', \'post type general name\'), /* This is the Title of the Group */
            \'singular_name\' => __(\'News\', \'post type singular name\'), /* This is the individual type */
            \'add_new\' => __(\'Add New\', \'custom post type item\'), /* The add new menu item */
            \'add_new_item\' => __(\'Add New\'), /* Add New Display Title */
            \'edit\' => __( \'Edit\' ), /* Edit Dialog */
            \'edit_item\' => __(\'Edit\'), /* Edit Display Title */
            \'new_item\' => __(\'New \'), /* New Display Title */
            \'view_item\' => __(\'View\'), /* View Display Title */
            \'search_items\' => __(\'Search news\'), /* Search Custom Type Title */
            \'not_found\' =>  __(\'Nothing found in the Database.\'), /* This displays if there are no entries yet */
            \'not_found_in_trash\' => __(\'Nothing found in Trash\'), /* This displays if there is nothing in the trash */
            \'parent_item_colon\' => \'\'
        ), /* end of arrays */
            \'description\' => __( \'This is the example custom post type\' ), /* Custom Type Description */
            \'public\' => true,
            \'publicly_queryable\' => true,
            \'exclude_from_search\' => false,
            \'show_ui\' => true,
            \'query_var\' => true,
            \'menu_position\' => 2, /* this is what order you want it to appear in on the left hand side menu */
            \'capability_type\' => \'post\',
            \'hierarchical\' => false,
            \'rewrite\' => array(\'slug\' => \'news\', \'with_front\' => true ),
            \'has_archive\' => true,
            /* the next one is important, it tells what\'s enabled in the post editor */
            \'supports\' => array( \'title\', \'editor\', \'thumbnail\')
        )
    );
}

4 个回复
SO网友:Firdaus Rudy

不需要从存档中的新查询开始。php循环

只需为自定义分类法和默认循环帖子创建不同的循环文件

有关更多详细信息,请参见此处Filtering a custom post type by custom taxonomy in archive template

示例I有分类法“服务”

档案文件php文件


<?php 
get_header();

if (is_post_type_archive(\'service\') || is_tax(\'services-category\') || is_tax(\'services-tags\')){
    get_template_part(\'my-loop-service\'); // Get Loop for Taxonomy service
} else {
   get_template_part(\'my-loop-post\'); // Default Loop for Post

}

    // Navigation
    echo \'<div id="my-navigation">\';
    the_posts_pagination( array(
        \'mid_size\' => 2,
        \'prev_text\' => __( \'Prev\', \'text-domain\' ),
        \'next_text\' => __( \'Next\', \'text-domain\' ),
    ));
    echo \'</div>\';

get_footer(); 
?>

SO网友:Svartbaard

功能

next_posts_link
以及

previous_posts_link
应在重置post数据之前调用。

SO网友:Dixita

分页链接后重置查询尝试以下操作:

<?php
    $paged = get_query_var(\'paged\') ? get_query_var(\'paged\') : 1;
    $args = array(\'post_type\' => \'news\', \'posts_per_page\' => 1, \'paged\' => $paged);
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
?>

// Loop

<?php endwhile; ?>

<div id="pagination" class="clearfix">
    <?php next_posts_link( \'Older Entries\', $loop->max_num_pages );
previous_posts_link( \'Newer Entries\' );//posts_nav_link(); ?>
</div>

<?php wp_reset_query();  ?>

SO网友:Milo

next_posts_linkprevious_posts_link 两个检查is_single 在这种情况下防止输出。它们用于归档页面,不适用于单一视图。

相关推荐

Count posts for pagination

我正在为一个网站分页<;上一页(页码)下一页>很简单,已经完成。但是现在我需要添加一个选择器来直接转到页面(例如:转到第7页),要这样做,我需要知道有多少页面,为此我需要计算在查询中找到了多少帖子。问题是这个网站有太多的帖子(>13.000),查询所有帖子都会减慢页面加载速度,这就像。。。10秒后页面才能加载。显然,这是不可接受的。分页解决了这个问题,因为一次只加载50或100篇文章,但我无法将它们全部计算在内。我可以在不加载的情况下统计某个查询中的帖子吗?或者我可以通过其他方式获得页数吗