0

Im trying to limit the number of post for my archive page, in a specific category.

I've manage to display post from just the category I desire,

However, if i setup a loop to limit the number of post, it doest work but display post from all time, instead of the specific time . . .

here is my code . ..

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

<?php if (!in_category('8')) continue; ?>

i tried to limit the number of post different variant such as

<?php static $count = 2;
if ($count == "n") { break; }
else { ?>

but nothing to do ..

Hope anybody can highlight me, thank you for your time

---------EDIT-----------

I add the fullcode in case it helps better ... thank you

<?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 Answer 1

1

You should use the pre_get_posts filter for this. For example, you could do this in your theme's functions.php file:

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' );
12
  • Does this will work only on my archive.php pge ? or also category ? My category page need to display a different number of post .. . Thank you for your time
    – tibewww
    Commented Nov 24, 2014 at 14:42
  • This will work for your category archive Commented Nov 24, 2014 at 14:43
  • any solution to make this work only on archive.php by any chance ?
    – tibewww
    Commented Nov 24, 2014 at 14:45
  • Please see my update Commented Nov 24, 2014 at 14:53
  • it seems to have an error in the code :( can you help me out ?
    – tibewww
    Commented Nov 24, 2014 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.