• What is the best concept to call a list of selected categories, with 5 recent posts – displaying the most recent as a post and the other 4 with featured image and title?
    Good example are magazine-themes.

    Category A (1)       Category B (1)
    ---------            -----------
    Feature image        Feature Image
    Title                Title
    content              content
    ---------------      ----------------
    []Category A (2)     []Category B (2)
    []Category A (3)     []Category B (3)
    []Category A (4)     []Category B (4)
    []Category A (5)     []Category B (5)

    I can’t grasp the whole multiple loop concept to put the whole structure together in one code.

    [can this post be moved to correct place, thanks]

Viewing 4 replies - 1 through 4 (of 4 total)
  • What is the best concept

    a custom page template http://codex.wordpress.org/Page_Templates with multiple loops based on WP_Query()http://codex.wordpress.org/Class_Reference/WP_Query

    and then a conditional statement based on the loop counter (for example $your_query->current_post) to distinguish between first and other posts…

    what theme are you working with?

    or alternatively, looping through an array with your categories, and calling just one loop.

    Thread Starter starbright

    (@starbright)

    Yes, I’ve read those links you’ve listed, The main confusion is where to start to get everything together in one loop? Multiple loops would seem best if only calling 2 or 3 categories?

    I’m not quite exactly working with “One” theme. Corpo responsive theme and Adapt theme I’ve tried to study so far.
    This is also to learn as I’ve found this concept everywhere in different premium themes.

    Thanks for your response Alchymyth, I’ve checked out your posts on similar topic.

    I’ve checked out your posts on similar topic.

    then you might have come across something like this (adapted to your question):

    <?php $cats = array( 'Category 1', 'Category 2' ); //use category titles//
    foreach ($cats as $cat) :
    
    $category = get_term_by( 'name', $cat, 'category' );
    $cat_id = $category->term_id;
    
    $args = array(
    'posts_per_page' => 5,
    'category__in' => array( $cat_id )
    );
    
    $cat_query = new WP_Query( $args ); ?>
    <div class="column">
    <?php if ( $cat_query->have_posts() ) :
    echo '<h2>Latest Posts in '.$category->name.' Category</h2>';
    while ( $cat_query->have_posts() ) : $cat_query->the_post(); ?>		
    
    <?php if( $cat_query->current_post == 0 ) : //first post// ?>
       <!-- this area is for the display of the first post
       Feature image
       Title
       content  etc. -->
       <?php else : ?>
       <!-- this area is for the display of the other posts
       Feature image
       Title -->
       <?php endif; ?>
    
    <?php endwhile; ?>
    <?php else :
    echo '<h2>No Posts for '.$category->name.' Category</h2>';
    endif;
    wp_reset_postdata(); ?>
    </div><!--/ .column -->
    <?php endforeach; ?>

    (not tested)

    Thread Starter starbright

    (@starbright)

    I’ve tried the code above, Seems to work as expected.
    Only one category is displayed with “No Posts for Category” at the end.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Multiple categories posts and latest titles’ is closed to new replies.