首页 > 解决方案 > wp_reset_postdate 未按预期重置

问题描述

我有一个 Wordpress 功能,用于我网站的两个页面、主页和类别存档。虽然此代码在类别存档上完美运行,但主页似乎存在 wp_reset_postdata() 问题 - 但我不知道如何修复它。已经尝试过 wp_reset_query() 但它似乎没有做任何不同的事情。

下面的代码输出特定类别中的帖子列表(对于此示例,我只是输出 ID)。我有一个单独的查询来检查帖子是否已被“固定”。如果有,它将嵌套在查询的位置 1(因此有效地将帖子输入到数组输出中)

if ( $wpb_all_query->have_posts() ) :

                while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
                    if( $count === 1 ) {
                        if ( $pinnedID ) {
                            while ( $wpb_pinned_content_query->have_posts() ) : $wpb_pinned_content_query->the_post();
                                echo $count . ": " . get_the_ID();
                                echo "<br/>";
                            endwhile;
                            wp_reset_postdata();
                                echo $count . ": " . get_the_ID();
                                echo "<br/>";
                        }
                        else {
                            echo $count . ": " . get_the_ID();
                            echo "<br/>";
                        }
                    }
                    else{
                        if($pinnedID === get_the_ID()){
                        }
                        else{
                            if ( $pinnedID ) {
                                if( $count === 0 || $count === 8 ) {
                                    echo $count . ": " . get_the_ID();
                                    echo "<br/>";
                                } 
                                else {
                                    echo $count . ": " . get_the_ID();
                                    echo "<br/>";
                                }
                            }
                            else{
                                if( $count === 0 || $count === 9 ) {
                                    echo $count . ": " . get_the_ID();
                                    echo "<br/>";
                                } 
                                else {
                                    echo $count . ": " . get_the_ID();
                                    echo "<br/>";
                                }   
                            }
                        }   
                    }                   
                    $count ++;
                endwhile;
                wp_reset_postdata();
endif;

如果没有“固定”帖子,则输出以下内容

0: 13402
1: 13383
2: 13409
3: 13397
4: 13361
5: 13332
6: 10886
7: 10884
8: 10862
9: 10795

如果有“固定”帖子,则输出;

0: 13402
1: 10619
1: 171 <- this is the ID of the homepage - it should be ID #13383
2: 13409
3: 13397
4: 13361
5: 13332
6: 10886
7: 10884
8: 10862
9: 10795

我知道这与 wp_reset_postdata() 有关 - 但我不知道如何解决它。有没有人有任何想法?

标签: wordpresswhile-looppostdata

解决方案


我自己设法弄清楚了这个

if ( $pinnedID ) {
    $backup=$post;
    $wpb_pinned_content_query = new WP_Query($innerargs);
    while ( $wpb_pinned_content_query->have_posts() ) : $wpb_pinned_content_query->the_post();
        echo $count . ": " . get_the_ID();
        echo "<br/>";
    endwhile;
    $post=$backup;
    if($pinnedID !== get_the_ID()) {
        echo $count . ": " . get_the_ID();
        echo "<br/>";
    }

}

通过声明二级查询内联然后将全局 $post 存储到备份变量中 - 我能够再次访问原始 $post 而无需调用 wp_reset_postdata();


推荐阅读