Help with query

admin2025-05-31  1

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 specific category. But I want to add one more thing and cant figure out how to do it. I want my homepage to first show 1 most recent post but only from specific categories and then followed by the rest as it already is. Is that even possible? 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 &#187;'); ?> 

<?php
endwhile; 
echo '</div>'; 
?>

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

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 specific category. But I want to add one more thing and cant figure out how to do it. I want my homepage to first show 1 most recent post but only from specific categories and then followed by the rest as it already is. Is that even possible? 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 &#187;'); ?> 

<?php
endwhile; 
echo '</div>'; 
?>

<?php else : 
echo 'No post published in:'.$cat->name;                
endif; 
wp_reset_query();
endforeach; 
?>
Share Improve this question asked May 8 at 19:16 TarahlynnTarahlynn 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Yes you can do this. Here's your PHP code wrapped inside an HTML structure with proper formatting and comments, ready to be used in a WordPress template file (like front-page.php or home.php). This example includes the logic to:

Show 1 most recent post from specific categories first

Then show 10 most recent posts from all categories

Then show 10 most recent posts per category

<?php
// 1. Show 1 most recent post from specific categories
$specific_cat_ids = array(3, 5, 7); // Replace with your category IDs

$args = array(
    'posts_per_page' => 1,
    'category__in' => $specific_cat_ids,
    'orderby' => 'date',
    'order' => 'DESC',
);

$featuredQuery = new WP_Query($args);
if ($featuredQuery->have_posts()) {
    echo '<section class="featured-post">';
    echo '<h1>Featured Post</h1>';
    while ($featuredQuery->have_posts()) {
        $featuredQuery->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>
        <div class="content">
          <?php the_content('Read More »'); ?>
        </div>

    <?php }
    echo '</section>';
    wp_reset_postdata();
}

// 2. Show 10 most recent posts from all categories
$args = array(
   'posts_per_page' => 10,
);
$customQuery = new WP_Query($args);
if ($customQuery->have_posts()) {
    echo '<section class="all-cats">';
    echo '<h1>Latest Posts</h1>';
    while ($customQuery->have_posts()) : $customQuery->the_post(); ?>

        <article>
          <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
          <div id="humbnail">
              <?php if (has_post_thumbnail()) {
                  the_post_thumbnail();
              } ?>
          </div>
          <div class="content">
            <?php the_content('Read More »'); ?>
          </div>
        </article>

    <?php
    endwhile;
    echo '</section>';
    wp_reset_postdata();
}

// 3. Show 10 most recent posts per category
$allcats = get_categories('child_of='.get_query_var('cat')); 

foreach ($allcats as $cat) :
    $args = array(
        'posts_per_page' => 10,
        'category__in' => array($cat->term_id)
    );

    $customInCatQuery = new WP_Query($args); 
    if ($customInCatQuery->have_posts()) : 
        echo '<section class="category-posts">';
        echo '<div id="posts-header-container"><h1><span>Recently from '.$cat->name.'</span></h1></div>';
        while ($customInCatQuery->have_posts()) : $customInCatQuery->the_post(); ?>

            <article>
              <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
              <div id="humbnail">
                  <?php if (has_post_thumbnail()) {
                      the_post_thumbnail();
                  } ?>
              </div>
              <div class="content">
                <?php the_content('Read More &#187;'); ?> 
              </div>
            </article>

        <?php
        endwhile; 
        echo '</section>'; 
    else : 
        echo '<p>No post published in: '.$cat->name.'</p>';                
    endif; 
    wp_reset_postdata();
endforeach; 
?>

How to Use: Replace the category IDs in $specific_cat_ids = array(3, 5, 7); with your desired category IDs.

Save this as your homepage template or insert the PHP part into your existing homepage template.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748632605a313665.html

最新回复(0)