【问题标题】:Accessing replaced content in a php while loop在 php while 循环中访问替换的内容
【发布时间】:2020-09-15 19:24:58
【问题描述】:

我在我的 Wordpress 网站上有一段代码,除了一件小事之外,它都能完美运行。

下面的代码输出特定类别中的帖子列表,我有一堆关于他们应该使用哪个模板的逻辑,一切正常。我有这个功能,如果一个帖子被“固定”,那个帖子应该在布局中的这个特定位置 - 再次工作得很好。唯一的问题是,如果有一个固定的帖子(它总是进入 1 的数组计数(从 0 开始)),那么现在应该在那里的帖子永远不会出现。我正在做的是用我的固定逻辑替换位置 1 的内容,而我真正想要做的是在位置 1 之前插入该固定逻辑

while ( $wpb_all_query->have_posts() ) : $wpb_all_query->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_the_ID() .'">';
                                        get_template_part( 'template-parts/content-min-ad', get_post_format() );
                                echo '</div>';
                            endwhile;
                            wp_reset_postdata();
                        }
                        else {
                            echo '<div class="grid-container-item '.$count . '-' . get_the_ID() .'">';
                                get_template_part( 'template-parts/content-min', get_post_format() );
                            echo '</div>';
                        }
                    }
                    else{
                        if($pinnedID === get_the_ID()){
                        //dont show article again
                        // echo "im the pinned article duplicate";
                        }
                        else{
                            if( $count === 0 || $count === 9 ) {
                                echo '  <div class="grid-container-item '.$count . '-' . get_the_ID() .'">';
                                    get_template_part( 'template-parts/content', get_post_format() );
                                echo '  </div>';
                            } 
                            else {
                                echo '<div class="grid-container-item '.$count . '-' . get_the_ID() .'">';
                                    get_template_part( 'template-parts/content-min', get_post_format() );
                                echo '</div>';
                            }
                        }   
                    }
                    
    
                    $count ++;
                endwhile;

关于如何让我在位置 1 的原始帖子输出的任何想法?

【问题讨论】:

    标签: php wordpress while-loop


    【解决方案1】:

    当 $count==1 时,您要么显示固定,要么首先显示...

    如果

    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_the_ID() .'">';
                get_template_part( 'template-parts/content-min-ad', get_post_format() );
                echo '</div>';
            endwhile;
            wp_reset_postdata();
        }
        // now display actual post #1
        if($pinnedID !== get_the_ID()) {
            echo '<div class="grid-container-item '.$count . '-' . get_the_ID() .'">';
            get_template_part( 'template-parts/content-min', get_post_format() );
            echo '</div>';
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 2017-05-28
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多