限制成就页面的帖子数量

时间:2014-11-24 作者:tibewww

我试图限制我的归档页面在特定类别中的帖子数量。

我已经设法从我想要的类别中显示帖子,

然而,如果我设置了一个循环来限制帖子的数量,它会一直显示帖子,而不是特定的时间。

这是我的代码。。

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

<?php if (!in_category(\'8\')) continue; ?>
我试图限制发布不同变体的数量,例如

<?php static $count = 2;
if ($count == "n") { break; }
else { ?>
但没什么可做的。。

希望任何人都能突出我,谢谢你的时间

---------编辑-----------

我添加了完整的代码,以防它更好地帮助。。。非常感谢。

<?php get_header(); ?>



<div id="main-home">

    <div id="home-left">

        <?php $featured_main = get_option(\'mm_slider_tags\'); if ($featured_main == "Select a tag:") { ?>

        <?php } else { ?>

        <div id="featured-container">

            <div class="flexslider">

                    <ul class="slides">

                    <?php $recent = new WP_Query(array( \'tag\' => get_option(\'mm_slider_tags\'), \'showposts\' => get_option(\'mm_slider_num\')  )); while($recent->have_posts()) : $recent->the_post();?>

                    <li>

                        <?php if (  (function_exists(\'has_post_thumbnail\')) && (has_post_thumbnail())  ) { ?>

                        <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(\'slider-thumb\'); ?></a>

                        <?php } else { ?>

                        <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo bloginfo(\'template_url\'); ?>/images/noimg.jpg" /></a>

                        <?php } ?>

                        <div class="featured-box">

                            <?php if(get_post_meta($post->ID, "maxmag_featured_headline", true)): ?>

                            <h2><a href="<?php the_permalink() ?>"><?php echo get_post_meta($post->ID, "maxmag_featured_headline", true); ?></a></h2>

                            <?php else: ?>

                            <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>

                            <?php endif; ?>

                            <p><?php echo excerpt(17); ?></p>

                        </div><!--featured-box-->

                    </li>

                    <?php endwhile; ?>

                </ul>

            </div><!--flexslider-->

        </div><!---featured-container-->

        <?php } ?>
<div class="home-widget">
<h3> IN SPORT</h3></div>
<div id="category2">

        <ul class="category2">




            <?php 
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if (!in_category(\'8\')) continue; ?>

            <li>
                <div class="archive2-image">
                    <?php if (  (function_exists(\'has_post_thumbnail\')) && (has_post_thumbnail())  ) { ?>
                    <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(\'small-thumb\'); ?></a>
                    <?php } ?>
                </div><!--archive2-image-->
                <div class="archive2-text">
                    <a href="<?php the_permalink() ?>" class="main-headline2"><?php the_title(); ?></a>
                    <p><?php echo excerpt(38); ?></p>
                    <div class="headlines-info">
                        <ul class="headlines2-info">
                            <li><?php the_time(\'F jS, Y\'); ?></li>
                        </ul>
                    </div><!--headlines-info-->
                </div><!--archive-text-->
            </li>
            <?php endwhile; endif; ?>
        </ul>

    </div><!--post-area-->

    </div><!--home-left-->

</div><!--main-home-->



<div id="home-right">
<div id="middle-widget">
<div class="middle_align">
        <ul class="middle-widget">
<div class="middle-160">
            <div class="image-contain">
        <?php if (  (function_exists(\'has_post_thumbnail\')) && (has_post_thumbnail())  ) { ?>

                        <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(\'slider-thumb\'); ?></a>

                        <?php } else { ?>

                        <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo bloginfo(\'template_url\'); ?>/images/noimg.jpg" /></a>

                        <?php } ?>

                        <div class="image-box">

                            <?php if(get_post_meta($post->ID, "maxmag_featured_headline", true)): ?>

                            <h2><a href="<?php the_permalink() ?>"><?php echo get_post_meta($post->ID, "maxmag_featured_headline", true); ?></a></h2>

                            <?php else: ?>

                            <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>

                            <?php endif; ?>
        </div>  

</div>

      <div class="middle-widget">
<h3>Latest News</h3>
</div>   
            <?php 
if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php if (!in_category(\'8\')) continue; ?>
      <ul class="middle_new">  

            <li>

                    <a href="<?php the_permalink() ?>" class="main-headline"><?php the_title(); ?></a>
                    <p><?php echo excerpt(10); ?></p>
                    <div class="headlines-info">

                    </div><!--headlines-info-->
            </li>
</ul>           <?php endwhile; endif; ?>
        </ul>

    </div><!--post-area-->
    </div>


<div id="bottom-widget">

    <?php if (!function_exists(\'dynamic_sidebar\') || !dynamic_sidebar(\'Homepage Bottom Widget Area\')): endif; ?>

</div><!--bottom-widget-->



<?php get_footer(); ?>

1 个回复
SO网友:henrywright

您应该使用pre_get_posts 对此进行筛选。例如,您可以在主题的函数中执行此操作。php文件:

function limit_category_posts( $query ) {
    if ( $query->is_archive() ) {

        // Do not do this for the date or category archives.
        if ( $query->is_date() || $query->is_category() )
            return $query;

        if ( $query->query_vars[\'cat\'] == 8 ) {
            $query->set( \'posts_per_page\', 2 );
        }
    }
    return $query;
}
add_filter( \'pre_get_posts\', \'limit_category_posts\' );

结束