简体   繁体   中英

How to display 13 post with specific category recent post in middle in each page of WordPress blog list page

在此处输入图像描述

I am trying to achieve this layout in Blog list page, little bit I achieved, but unable to display recent post of specific category into each page, how can I achieve, here is my code

         <?php
        $args = array(  
            'post_type' => 'post',
                    'posts_per_page' => 13,
        );
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $loop = new WP_Query( $args ); 
        $count = 0;
        while ( $loop->have_posts() ) : $loop->the_post(); $count++ ?>
    <?php if( $count % 7 === 0 ){ ?>
    <div class="col-md-12">
        
        <?php the_title(); ?>
    </div>
    <?php } else { ?>
    
    
<div class="col-md-4">
<?php the_title(); ?>
    </div>
 <?php } ?>
     <?php endwhile; ?>
    <div class="d-flex justify-content-center">
    <nav class="pagination ">
        <?php pagination_bar( $loop ); ?>
    </nav>
    </div>
     <?php  wp_reset_postdata(); 
    ?>

if you don't what to have duplicated posts in your loop, first you need to exclude the "Featured" category from the main query, you can use pre_get_posts action for this. Use it to set the main query number of posts ( 'posts_per_page' = 12 ), or do it from /wp-admin/options-reading.php

Then every 7nth post in your main loop, do a custom query on your "featured" category for only 1 post per page.

<?php
$count = 0;
while ( have_posts() ) : the_post(); $count++;
    if(7 === $count) :
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $featured = get_posts(
            array(
                'category'       => $featured_category_id,
                'posts_per_page' => 1,
                'paged'          => $paged,
            )
        );

        setup_postdata($featured);

        // Display the featured post

        wp_reset_postdata();

    else : 
        // Display regular post;
    endif;
endwhile;  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM