wp_reset_postdate 未按预期重置 [英] wp_reset_postdate not reseting as expected

查看:23
本文介绍了wp_reset_postdate 未按预期重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Wordpress function that is used within two pages of my site, home page and category archive. Whilst this code works perfectly on the category archive, the home page seems to have issues with wp_reset_postdata() - but i don't know how to fix it. have tried wp_reset_query() but it doesn't seems to do anything different.

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

This code below outputs a list of the posts within a specific category (for this example, i'm just outputting the IDs). I have a separate query that checks if a post has been 'pinned'. If it has, it nests in position 1 of the query (so effectively inputting a post into the array output)

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;

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

if there are no 'pinned' posts, the following is outputted

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

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

if there is a 'pinned' post this is outputted;

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() 有关 - 但我不知道如何修复它.有人有什么想法吗?

I know it's to do with the wp_reset_postdata() - but i don't know how to fix it. Does anyone have any ideas?

推荐答案

我已经设法自己解决了这个问题

I've managed to figure this one out myself

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();

By declaring the secondary query inline then storing the global $post into a backup variable - i was able to access the original $post again without having to call wp_reset_postdata();

这篇关于wp_reset_postdate 未按预期重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆