Reputation: 5
I have a WordPress site that displays the most recent posts. They are output in a design that looks similar to this:
http://tienvooracht.nl/themes/elevate/
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?
Upvotes: 0
Views: 2263
Reputation: 133
<?php
/* create categories array and pass id of categories from which we want to show the posts*/
$categories=array(4,5,3);
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);
$myposts=get_posts($args);
foreach($myposts as $mypost){
echo "<h1>".$mypost->post_title."</h1> ".$mypost- >post_content." ".$mypost->post_modified;
echo "<br />";
}
}
?>
Upvotes: 0
Reputation: 180
For limiting the post from specific category use this code.
<?php
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...
Upvotes: 1
Reputation: 749
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.
<?php
$defaultPostsPerPage = 12;
$sndary = $defaultPostsPerPage - $x;
if($sndary > 0){
$y=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 -->
<?php
if($y%4==0){
//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.
Upvotes: 0