Display Latest Posts from Multiple Categories

Community Forums Forums Design Tips and Tricks Display Latest Posts from Multiple Categories

This topic is: resolved

This topic contains 10 replies, has 3 voices, and was last updated by  e96989955 1 year, 8 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #7645

    e96989955
    Participant
    Post count: 15

    Hi, 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? :(

    #7671

    nickthegeek
    Keymaster
    Post count: 751

    This is a bit tricky. For you have to remove the standard loop by adding this code to the home.php file
    [php]
    remove_action( ‘genesis_loop’, ‘genesis_do_loop’ );
    [/php]
    Then you have to make your own custom loop by using
    [php]
    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.

    }
    [/php]

    You will need to remove the pagination as well so also add this to the home.php file
    [php]
    remove_action( ‘genesis_after_endwhile’, ‘genesis_posts_nav’ );
    [/php]

    #7835

    e96989955
    Participant
    Post count: 15

    Currently 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’ );

    #7840

    e96989955
    Participant
    Post count: 15

    Sorry, I post it thrice by accident and  I can’t seem to find the ‘delete’ post button here..

    #7841

    e96989955
    Participant
    Post count: 15

    Urgh, 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.

    #8046

    RossTeasley
    Participant
    Post count: 3
    This reply has been marked as private.
    #8047

    RossTeasley
    Participant
    Post count: 3

    Minor 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.

    #8338

    e96989955
    Participant
    Post count: 15

    Hi 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.

    #8345

    nickthegeek
    Keymaster
    Post count: 751

    you will need to add a clear break after each row. The HTML is
    [html]
    <br class="clear" />
    [/html]

    #8506

    e96989955
    Participant
    Post count: 15

    Hi 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.

    #8509

    e96989955
    Participant
    Post count: 15

    Sorry, my bad! I forgot to add backslashes before each double quote on the break HTML code.

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.