为自定义帖子类型模板创建简单的分页

时间:2015-02-01 作者:Jack Barham

我有自定义的post-post-type调用“news”,我正在努力让它正确分页。我正在寻找一个简单的上一页和下一页的链接,不担心中间的在中间。

在页面新闻中。php这是我的代码:

<?php
    $paged = get_query_var(\'paged\') ? get_query_var(\'paged\') : 1;
    $args = array(\'post_type\' => \'news\', \'posts_per_page\' => 2, \'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 posts_nav_link(); ?>
</div>
上面的代码显示了2篇文章,但没有分页链接

这是我的自定义帖子类型代码:

function custom_post_news() {
    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 ),
            /* the next one is important, it tells what\'s enabled in the post editor */
            \'supports\' => array( \'title\', \'editor\', \'thumbnail\')
        )
    );

}


// REGISTER TAXOMONIES

add_action( \'init\', \'custom_post_news\');
register_taxonomy( \'custom_news\',
    array(\'news\'), /* if you change the name of register_post_type( \'movies\', then you have to change this */
    array(\'hierarchical\' => true,
        \'labels\' => array(
            \'name\' => __( \'News Categories\' ), /* name of the custom taxonomy */
            \'singular_name\' => __( \'news Category\' ), /* single taxonomy name */
            \'search_items\' =>  __( \'Search news Categories\' ), /* search title for taxomony */
            \'all_items\' => __( \'All news Categories\' ), /* all title for taxonomies */
            \'parent_item\' => __( \'Parent news Category\' ), /* parent title for taxonomy */
            \'parent_item_colon\' => __( \'Parent news Category:\' ), /* parent taxonomy title */
            \'edit_item\' => __( \'Edit news Category\' ), /* edit custom taxonomy title */
            \'update_item\' => __( \'Update news Category\' ), /* update title for taxonomy */
            \'add_new_item\' => __( \'Add New news item\' ), /* add new title for taxonomy */
            \'new_item_name\' => __( \'New Custom news\' ) /* name title for taxonomy */
        ),
        \'show_ui\' => true,
        \'query_var\' => true,
    )
);
这是一个自定义主题(从头开始),所以我的函数中没有任何内容。php引用分页。我在互联网上搜寻答案,但我得到了不同的解决方案,似乎什么都不起作用。

我也没有新闻档案。我不确定这是否需要实现?

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

posts_nav_link() 这里不行

用于提供索引、类别和归档页面的“分页”导航。

您必须使用更通用的next_posts_link()previous_posts_link(). 只需确保设置$max_pages 的参数next_posts_links() 否则它将不起作用,请记住,如果通过更改,您将此页面用作静态首页,那么您应该使用page, 不paged

编辑1使用上述链接,如下所示

next_posts_link( \'Older Entries\', $loop->max_num_pages );
previous_posts_link( \'Newer Entries\' );
编辑2我想有一点误解。请尝试以下操作:

在注册自定义帖子类型的自定义帖子类型参数中,添加以下内容,\'has_archive\' => true. 您的函数应该如下所示

function custom_post_news() {
    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\')
        )
    );

}
添加后刷新永久链接,然后访问主页

创建archive-news.php. 应该是这样的

<?php
    while ( have_posts() ) : the_post();
?>

// Loop

<?php endwhile; ?>

<div id="pagination" class="clearfix">
    <?php posts_nav_link(); ?>
</div>
下一步,在函数中。php,使用pre_get_posts 根据需要更改主查询。

add_action( \'pre_get_posts\', function ( $q ) {

    if( !is_admin() && $q->is_main_query() && $q->is_post_type_archive( \'news\' ) ) {

        $q->set( \'posts_per_page\', 2 );

    }

});
删除您在后端创建的页面。现在一切都应该很好

结束

相关推荐

Pagination and Related Posts

是否可以对嵌入在single中的以下相关POST代码使用分页。我的主题的php。 <?php // for use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { $first_tag = $