php 限制每个类别/存档页面的帖子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 限制每个类别/存档页面的帖子相关的知识,希望对你有一定的参考价值。


// The below code will limit the number of posts shown on ALL categories. Simply copy and paste it into your themes functions.php file.
add_filter('pre_get_posts', 'limit_category_posts');
function limit_category_posts($query){
    if ($query->is_category) {
        $query->set('posts_per_page', 5);
    }
    return $query;
}


// This can also apply to archives, just change the word “category” to archive, like this:
add_filter('pre_get_posts', 'limit_archive_posts');
function limit_archive_posts($query){
    if ($query->is_archive) {
        $query->set('posts_per_page', 5);
    }
    return $query;
}


// Or if you would like to limit the number of posts within a particular category:
add_filter('pre_get_posts', 'per_category_basis');
function per_category_basis($query){
    if ($query->is_category) {
        // category named 'books' show 12 posts
        if (is_category('books'){
            $query->set('posts_per_page', 12);
        }
        // category With ID = 32 show only 5 posts
        if (is_category('32'){
            $query->set('posts_per_page', 5);
        }
    }
    return $query;
}


// This can also be achieved by placing the following code before the loop in the category/archive template file…
global $query_string;
query_posts("{$query_string}&posts_per_page=5");

以上是关于php 限制每个类别/存档页面的帖子的主要内容,如果未能解决你的问题,请参考以下文章

WordPress中单个类别的存档页面

如何:在wordpress中自定义帖子类型的自定义类别存档页面

php 显示每个分类术语存档页面中的所有帖子

突出显示存档中的类别 - WordPress

php 如果是自定义帖子类型类别 - 存档

链接到显示所有帖子的存档页面