How can I set the limit number of the posts of a specify category into a page of that category?

倖福魔咒の 提交于 2019-12-11 16:21:20

问题


I would to know how can I set the limit number of the posts visible into a page of a specify category.


回答1:


Put this in your functions.php

function main_query_mods( $query ) {
    if(!$query->is_main_query()) {
        return;
    }
    // show 15 posts per page if category has id 7
    // check http://codex.wordpress.org/Conditional_Tags#A_Category_Page
    if ( is_category('7')) {
        $query->set('posts_per_page',15);
    }
}
add_action( 'pre_get_posts', 'main_query_mods' );



回答2:


You should use this:

<?php $the_query = new WP_Query ('cat=7&posts_per_page=15'); ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
    <div><?the_title(); //display post title?></div>
    <div><?the_content(); //display post content?></div>
<?php endwhile; ?>

cat is the category id
post_per_page is the limit if the posts

Adding &orderby=rand to the query will be select random posts.

You can find the full documentation of this here: http://codex.wordpress.org/Class_Reference/WP_Query



来源:https://stackoverflow.com/questions/10152921/how-can-i-set-the-limit-number-of-the-posts-of-a-specify-category-into-a-page-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!