如何将指定类别的帖子限制数设置为该类别的页面?

时间:2012-04-14 10:38:47

标签: wordpress limit categories posts

我想知道如何在指定类别的页面中设置可见帖子的限制数量。

2 个答案:

答案 0 :(得分:1)

将它放在你的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' );

答案 1 :(得分:0)

你应该用这个:

<?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是类别ID
如果帖子为
post_per_page是限制 将<{1}}添加到查询中将选择随机帖子。

您可以在此处找到完整的文档:http://codex.wordpress.org/Class_Reference/WP_Query