Limit how many posts display in WordPress based on category

I have a WordPress site that displays the most recent posts. They are output in a design that looks similar to this:

Three columns with four rows of content. Here is the part that I can't figure out - how do I pull the most recent posts regardless of category, but limit how many are shown per row?

Example: I have the categories, Photography, Web Design and UI Design. I happen to write 5 posts about Photography in a row, but I don't want all my most recent posts to be about Photography.

Is there a way to limit how many posts from a single category are displayed and show other posts from a different category once that limit has been reached?


Is that limit static? as in always the same. If so, you can set it in the reading settings of WordPress. Before the main loop add a variable $x=0; and then in the loop add $x++; Then just add a 2nd query under like so.


$defaultPostsPerPage = 12;

$sndary = $defaultPostsPerPage - $x;

if($sndary > 0){
$args = array('post_type'=>'post','post_per_page'=>12);

$showcase = new WP_Query( $args );

if ( $showcase->have_posts() ) { while ( $showcase->have_posts() ) { $showcase->the_post(); ?>
<!-- post html css goes here as you would any other loop -->
        //add some sort of css line break, clear fix or last class on the element. to break to the new line. 
} ?>
<!-- post navigation -->
<?php }else{ ?>
<!-- no posts found -->
<?php }


Here we're checking how many posts there were in the main query, if they're under the max per page we can add our secondary query. The only thing to note here is that we're not accounting for pagination, but that wasn't part of the question.

For limiting the post from specific category use this code.

global $post;
$args = array( 'numberposts' => 5, 'category' => 3 );

$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post); ?>

<?php the_title(); ?>
<?php the_content(); ?>

<?php endforeach; ?>

Change the category ID and number of post limit...

/* create categories array  and pass id of categories from which we want to show the       posts*/
foreach($categories as $cat){
    global $post;
    /* limit numberposts, here  specified 2  and pass the categories   dynamically in args array*/
    $args=array('numberposts' =>2, 'category' =>$cat);
    foreach($myposts as $mypost){
            echo "<h1>".$mypost->post_title."</h1>   ".$mypost-   >post_content."   ".$mypost->post_modified;
            echo "<br />";


Need Your Help

How to direct to an .aspx web form from a controller (MVC 5 framework)? forms redirect

I have a MVC project which needs to incorporate SSRS reports as a part of the application. I've not done this before so I read up a couple of blogs, msdn pages etc. But I haven't come across a conc...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.