Community Forums › Forums › Archived Forums › Design Tips and Tricks › Display Latest Posts from Multiple Categories
Tagged: Multiple Categories
- This topic has 10 replies, 3 voices, and was last updated 12 years, 3 months ago by
e96989955.
-
AuthorPosts
-
December 26, 2012 at 10:22 pm #7645
e96989955
MemberHi, I'm using the Expose theme and would like to display the 3 latest posts from each category on my home page like this site here: http://www.newlaunchguru.com
I think it's a relatively simple feature but can't seem to find a plugin or hack in the WordPress community. Can anyone help me? 🙁
December 27, 2012 at 8:30 am #7671nickthegeek
MemberThis is a bit tricky. For you have to remove the standard loop by adding this code to the home.php file
remove_action( 'genesis_loop', 'genesis_do_loop' );
Then you have to make your own custom loop by using
add_action( 'genesis_loop', 'child_do_home_loop' ); function child_do_home_loop() { //change "x" to match your category ID $loop_args = array( 'cat' => 'x', 'posts_per_page' => 3 ); genesis_custom_loop( $loop_args ); //Change "y" to match your category ID $loop_args['cat'] = 'y'; genesis_custom_loop( $loop_args ); //repeat the last two lines for each category. }
You will need to remove the pagination as well so also add this to the home.php file
remove_action( 'genesis_after_endwhile', 'genesis_posts_nav' );
December 27, 2012 at 9:47 pm #7835e96989955
MemberCurrently my code in home.php looks like this
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'child_do_home_loop' );
function child_do_home_loop() {Echo "<h1>Heading 1</h1>";
//change "x" to match your category ID
$loop_args = array( 'cat' => '1', 'posts_per_page' => 3 );genesis_custom_loop( $loop_args );
Echo "<h1>Heading 2</h1>";
//Change "y" to match your category ID
$loop_args['cat'] = '4';genesis_custom_loop( $loop_args );
Echo "<h1>Heading 3</h1>";
//Change "y" to match your category ID
$loop_args['cat'] = '6';genesis_custom_loop( $loop_args );
Echo "<h1>Heading 4</h1>";
//Change "y" to match your category ID
$loop_args['cat'] = '7';genesis_custom_loop( $loop_args );
Echo "<h1>Heading 5</h1>";
//Change "y" to match your category ID
$loop_args['cat'] = '5';genesis_custom_loop( $loop_args );
}
remove_action( 'genesis_after_endwhile', 'genesis_posts_nav' );
December 27, 2012 at 9:57 pm #7840e96989955
MemberSorry, I post it thrice by accident and I can't seem to find the 'delete' post button here..
December 27, 2012 at 10:00 pm #7841e96989955
MemberUrgh, my original question got eoverwritten by the code snippet I pasted somehow.
I tried to add a heading before displaying each category, but it turned out rather weird like this : http://www.thenewlaunchguru.com
My original intention is to have each category displayed in the next line. Some categories have 3 entries, whilst some is just 1 or 2. The rest are empty at the moment.
December 28, 2012 at 6:37 pm #8046RossTeasley
MemberThis reply has been marked as private.December 28, 2012 at 6:38 pm #8047RossTeasley
MemberMinor correction in NickTheGeek's code above:
$loop_args = array( 'cat' => 'x', 'posts_per_page' => 3 );
should read:
$loop_args = array( 'cat' => x, 'posts_per_page' => 3 );
(without apostrophes around the category id value)... at least that's was needed on my server. This applies to each line that sets the cat ID.
December 30, 2012 at 7:02 am #8338e96989955
MemberHi Ross!
Thanks for your reply. 🙂 I think you misunderstood my question. What I wanted was for each category to be displayed in a new row in the grid layout for the Expose child theme that I'm using.
Currently, if my category has less than 3 entries, it'll be squashed side by side with the entries from the previous category.
December 30, 2012 at 7:48 am #8345nickthegeek
Memberyou will need to add a clear break after each row. The HTML is
<br class="clear" />
December 30, 2012 at 8:39 pm #8506e96989955
MemberHi nick, I get a HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request. when I insert the Break as an echo before each heading.
December 30, 2012 at 9:02 pm #8509e96989955
MemberSorry, my bad! I forgot to add backslashes before each double quote on the break HTML code.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.