• Tarahlynn

    (@tarahlynn)


    Hey guys so currently on my homepage I have it setup so it first shows the most recent 10 posts (of all categories) and then that’s followed by the most recent 10 posts of each individual category.

    But I want to add one more thing and cant figure out how to do it. I want to first show the 1 most recent post from only specific categories and then followed by the rest as it already is. Am I asking too much lol Here’s the code (Thank you guys so much in advance!):

    <?php  
    $args = array(
    'posts_per_page' => 10,
    );
    $customQuery = new WP_Query( $args );
    if ( $customQuery->have_posts()) {
    echo '<div class="all-cats">';
    while ( $customQuery->have_posts()) : $customQuery->the_post(); ?>

    <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>
    </div>
    <div id="posts_clear">
    <div id="posts_over"><!-- .all-cats -->';
    } //endif

    $allcats = get_categories('child_of='.get_query_var('cat'));

    foreach ($allcats as $cat) :
    $args = array(
    'posts_per_page' => 10, // 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"><h1><span>Recently from '.$cat->name.'</span></h1></div>';
    echo '';
    while ($customInCatQuery->have_posts()) : $customInCatQuery->the_post(); ?>

    <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>';
    ?>

    <?php else :
    echo 'No post published in:'.$cat->name;
    endif;
    wp_reset_query();
    endforeach;
    ?>

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • Dillon Lanier

    (@dillonlanier)

    Where are you getting stuck? You should be able to do a similar loop to the last one that goes through each category and shows the 10 most recent for each category. The modifications would be:

    • posts per page is 1 instead of 10
    • title outside the foreach loop
    • place at top of the file

    So, copy all the code from ‘$allcats = get_categories(‘child_of=’.get_query_var(‘cat’));’ to the bottom, put it at the top, move the header outside the foreach loop, and change 10 to 1. This should get you pretty close, though you very possibly may need to tweak things.

    bvbaked

    (@bvbaked)

    A couple of things to note here:

    $allcats = get_categories('child_of='.get_query_var('cat')); 

    This is expecting a query parameter to narrow down your categories by a parent, which I’m not quite sure you’re looking to do. The url in this case would look like http://www.grandmashousediy.com/?cat=5 that actually pushes you to the category page, so you may want to just remove the string inside as it is functioning as you expect already – it gets all categories, then loops through each to get the 10 most recent to display

    For the next ask of displaying the most recent only from specific categories:
    That depends on how you’d like to select them. If you have the categories in mind and that won’t change, you can write another query similar to what you have already but only choosing certain ids.

    Something like

    $args = [ 'taxonomy' => 'category', 'include' => [ 3, 4, 5 ] ];
    $categories_to_show = get_terms( $args );

    Repeat the loop on your $customInCatQuery loop replacing it with the result here and posts_per_page with 1 instead of 10 – this would then be giving you the first in each of those categories.

    Place that where you’d want the output, and I think you’re good.

    Let me know if I misunderstood though 🙂

    WP SITES

    (@wordpresssites)

    $category_ids = array(4, 7, 10); // Replace with your desired category IDs

    foreach ( $category_ids as $cat_id ) {
    $args = array(
    'post_type' => 'post',
    'posts_per_page' => 1,
    'orderby' => 'date',
    'order' => 'DESC',
    'cat' => $cat_id,
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
    $query->the_post();
    echo '<h3>Category: ' . get_cat_name( $cat_id ) . '</h3>';
    echo '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';
    }
    wp_reset_postdata();
    }
    }
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Query recent posts’ is closed to new replies.