Saturday, December 24, 2022
HomeWordPress Developmentwp question - The way to insert a publish from a special...

wp question – The way to insert a publish from a special publish kind after each nth publish


The thought appears easy sufficient, insert a publish from a special publish kind after each third publish in a wp_query. So ought to look one thing like this:

Article 1
Article 2
Article 3
Advert 1
Article 4
Article 5
Article 6
Advert 2
Article 7
Article 8
Article 9
Advert 3

Within the instance above, the 2 completely different publish varieties are ‘Article’ and ‘Advert’. I can get the fundamental setup utilizing modulo operator, utilizing both 2 wp_queries, or with a single one like within the code instance under. The problem I am working into is that I can not seem to get the Advert loop to iterate appropriately, when utilizing two completely different loops I am not in a position to cross the 2nd Advert after the sixth Article, it can simply all dump on the 1st Advert place ($counter = 4)

<?php

//   $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$get_posts = new WP_Query(array(
    'post_type'         => array ( 'articles', 'adverts' ),
    'post_status'       =>'publish', 
    'posts_per_page'    =>-1
));

$counter = 1;

if ( $get_posts -> have_posts() ) : 
    whereas ( $get_posts -> have_posts() ) : $get_posts -> the_post();

        if ( $counter % 3 === 0 && $counter > 1 ) {
            if ( get_post_type() == 'adverts') {
    
                echo the_title();
        
            }
        }

        $counter++;

    endwhile;

else : ?>
    <p class="discover"><?php _e( 'There are not any posts to show in the intervening time.' ); ?></p>
<?php endif; ?>

Is it appropriate to run each publish varieties by a single loop, or ought to I break them aside into completely different loops to appropriately iterate? Thanks.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments