简体   繁体   中英

insert item into wordpress the_posts() while loop

I have a project in Wordpress and am outputting the posts on the homepage (layout is a bit complex, so there are a few if statements to determine which template-parts to use).

However i have a piece of functionality which is to display 1 specific post (toggled in the admin area) in a certain place - basically pinning the post to always display in the first array position.

I have two problems with the current way i've programmed this;

1 - the pinned article is replacing the post in that current position in the array, so the post in position one never gets displayed

2 - the pinned article may well appear in the_posts() so i don't want it displayed twice.

Whats the best way of dealing with these problems? I feel like there must be a better way

            $count = 0;
            $rows = 10;
            echo '<div class="grid-container grid-rows-' . $rows . '"">';
            while ( have_posts() ) : the_post();
                
                if($count === 0 || $count === 9 || $count === 10 || $count === 19){
                    echo '  <div class="grid-container-item '.$count.'">';
                                get_template_part( 'template-parts/content', get_post_format() );
                    echo '  </div>';
                }
                else{
                    if($count === 1) {
                        //display pinned_featurette content
                        if ( $wpb_pinned_content_query->have_posts() ) :
                            while ( $wpb_pinned_content_query->have_posts() ) : $wpb_pinned_content_query->the_post();
                                    echo '<div class="grid-container-item pinned_ad">';
                                            get_template_part( 'template-parts/content-min-ad', get_post_format() );
                                    echo '</div>';
                            endwhile;
                            wp_reset_postdata();
                        //if none - place normal content
                        else :
                            
                            echo '<div class="grid-container-item '.$count.'">';
                                get_template_part( 'template-parts/content-min', get_post_format() );
                            echo '</div>';
                        endif;
                    }
                    else {
                        echo '<div class="grid-container-item '.$count.'">';
                            get_template_part( 'template-parts/content-min', get_post_format() );
                        echo '</div>';
                    }
                }
                $count ++;
            endwhile;

            echo '</div>';
  1. Exclude the pinned posts IDs from the homepage Query

     add_action( 'pre_get_posts', function( $query ) { if ( $query->is_main_query() && $query->is_home() ) { $query->set( 'post__not_in', array( 1, 2, 3, '# IDs of the pinned posts' ) ); } }, 100, 1 );
  2. to avoid replacing the post in the same position in the main loop

    $count = 0; $rows = 10; echo '<div class="grid-container grid-rows-' . $rows . '"">'; while ( have_posts() ) : the_post(); if( $count === 1 ) { //display pinned_featurette content if ( $wpb_pinned_content_query->have_posts() ) : while ( $wpb_pinned_content_query->have_posts() ) : $wpb_pinned_content_query->the_post(); echo '<div class="grid-container-item pinned_ad">'; get_template_part( 'template-parts/content-min-ad', get_post_format() ); echo '</div>'; endwhile; wp_reset_postdata(); endif; } if( $count === 0 || $count === 9 || $count === 10 || $count === 19 ) { echo ' <div class="grid-container-item '.$count.'">'; get_template_part( 'template-parts/content', get_post_format() ); echo ' </div>'; } else { echo '<div class="grid-container-item '.$count.'">'; get_template_part( 'template-parts/content-min', get_post_format() ); echo '</div>'; } $count ++; endwhile; echo '</div>';

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