• Resolved Tarahlynn

    (@tarahlynn)


    Hi guys, so I’ve been banging my head on this for awhile now. Currently on my home page I am showing the most recent three posts from all of my categories. I love it, it is exactly what I want. However I would also like to have above that my most recent three posts from ALL categories, is it possible to alter my code a bit to include that too? Here is the code.

    <?php  
     $allcats = get_categories('child_of='.get_query_var('cat')); 
    
        foreach ($allcats as $cat) :
        $args = array(
        'posts_per_page' => 3, // max number of post per category
    
        'category__in' => array($cat->term_id)
        );
    
        $customInCatQuery = new WP_Query($args); 
        if ($customInCatQuery->have_posts()) : 
        echo '<div>';
        echo '<div id="posts-header-container"><h6><span>Most recent from '.$cat->name.'</span></h6></div>';
        echo '';    
        while ($customInCatQuery->have_posts()) : $customInCatQuery->the_post(); ?>
    
    <div class="date"><strong><?php the_time('M d Y'); ?></strong></div>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    	
    <div id="humbnail">
              		<?php 
                          if ( has_post_thumbnail() ) {
                          	the_post_thumbnail();
                          } 
                          ?>  			
    </div>
                          <?php the_content('Read More »'); ?>	
    
    <?php
    endwhile; 
    echo '</div>'; 
    ?>

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Almost anything is possible with adequate custom coding ??

    What you want is certainly possible. But it can get a little complicated if it’s important for the recent posts in all categories should not also appear in the individual category listings. In other words, if there should never be a duplicated post anywhere on your home page in any of the listings, it gets a little complicated. That could happen with your current code if a single post were to be assigned more than one category.

    Even if more complicated, it can be handled if necessary. I’m going to assume the simpler case where duplicates are OK. The code can be modified further if you decide the duplication is bothersome.

    What you need to do is add another new WP_Query with associated loop code above your current category code, except omitting anything related to categories.

    <?php  
        $args = array(
           'posts_per_page' => 3,
        );
        $customQuery = new WP_Query( $args ); 
        if ( $customQuery->have_posts()) { 
          echo '<div class="all-cats">';
            while ( $customQuery->have_posts()) : $customQuery->the_post(); ?>
    
    <div class="date"><strong><?php the_time('M d Y'); ?></strong></div>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    	
              <div id="humbnail">
              		<?php 
                          if ( has_post_thumbnail() ) {
                          	the_post_thumbnail();
                          } 
                          ?>  			
              </div>
                          <?php the_content('Read More ?'); ?>	
            <?php
            endwhile;
          echo '</div><!-- .all-cats -->';
        } //endif
    $allcats = get_categories( // the rest of your current code.... 
    Thread Starter Tarahlynn

    (@tarahlynn)

    You’re amazing, that is absolutely perfect, I actually wanted it to have duplicate posts. Thank you SO much!

    Moderator bcworkz

    (@bcworkz)

    Great! You’re welcome.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Recent posts plus recent posts for all of my categories’ is closed to new replies.