简体   繁体   中英

WordPress limit posts in category page

This is my WordPress loop code... :)

<?php if(have_posts()) : ?>
        <?php while(have_posts()) : the_post(); ?>
        <?php if (is_category('instagram')) {?>

        <?php
            $__width = '225';
            $__height = '225';
        ?>
            <div id="instagram-photos">
                <a href="<?php the_permalink() ?>"class="instagram-photo">
                <div class="photo"><?php custom_get_post_attachments(get_the_ID(), $__width, $__height, get_the_title()); ?>    </div>
                </a>
            </div>

            <?php }elseif(is_category('runway')) { ?>
            <?php
            $__width = '298';
            $__height = '500';
            ?>
            <div id="runway-category">
                <a href="<?php the_permalink() ?>" class="runway-category-posts">
                <div class="photo"><?php custom_get_post_attachments(get_the_ID(), $__width, $__height, get_the_title()); ?><div class="runway-title"><?php the_title(); ?></div></div>
                </a>            
            </div>
            <?php } elseif(is_category('')){?>
        <article <?php post_class(); ?>>
                    <div class="latest-posts">
                    <div class="latest-posts-info">
                    <div class="title"><h1><?php the_title(); ?><h1></div>
                    <div class="text">
                    <?php the_excerpt(); ?>
                    </div>
                    <div class="post-share">
                        <div class="facebook-like" style="float:left;">
                            <a href="http://www.facebook.com/sharer.php?u=<?php the_permalink() ?>" target="_blank"><img src="<?php echo get_template_directory_uri(); ?>/images/facebook-icon.png" /></a>
                        </div>
                        <div class="twitter-follow" style="float:left;">
                            <a href="http://twitter.com/share?text=<?php the_title(); ?>&url=<?php the_permalink() ?>" target="_blank" ><img src="<?php echo get_template_directory_uri(); ?>/images/twitter-icon.png" /></a>
                        </div>
                        <div class="google-share">
                            <a href="https://plus.google.com/share?url=<?php the_permalink() ?>" target="_blank" ><img src="<?php echo get_template_directory_uri(); ?>/images/google-icon.png" /></a>
                        </div>
                    </div>
                    <div class="clear"></div><div class="post-read-more"><a href="<?php the_permalink() ?>" class="read-more">Read More</a></div>
                    <div class="clear"></div>
                </div>
                <div class="latest-posts-img">
                    <?php //echo get_the_post_thumbnail(); ?>
                    <?php custom_get_post_attachments(get_the_ID(), $__width, $__height, get_the_title()); ?>                   
                </div>
                <div class="clear"></div>
                </div>
        </article>
        <?php }?>
        <?php endwhile; else: ?>
        <div class="content">
        <p class="not-found-p">No articles found!</p>
        </div>
        <?php endif; ?>

As you see I have few conditions as is_category('instagram') .

The thing is: I need to limit the post quantity in exact category, not at all them.
How can I do that?

i am uncertain if we can do this in a wordpress way .. but here is the workaround --

$no_of_posts = 5;

$post_counter = 0;

if(is_Category('instagram') && ($no_of_posts<=$post_counter)) {
// bunch of code 
$post_counter++;
}

untested but something like could get you going

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM