简体   繁体   English

将项目插入 wordpress the_posts() while 循环

[英]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).我在 Wordpress 中有一个项目,正在输出主页上的帖子(布局有点复杂,因此有一些 if 语句来确定要使用哪些模板部分)。

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.但是,我有一个功能是在某个位置显示 1 个特定的帖子(在管理区域中切换) - 基本上是将帖子固定为始终显示在第一个阵列位置。

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 1 - 固定文章正在替换数组中当前位置的帖子,因此位置一的帖子永远不会显示

2 - the pinned article may well appear in the_posts() so i don't want it displayed twice. 2 - 固定的文章很可能出现在 the_posts() 中,所以我不希望它显示两次。

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从主页查询中排除固定的帖子 ID

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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