wordpress - 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() 有关 - 但我不知道如何解决它。有没有人有任何想法?
解决方案
我自己设法弄清楚了这个
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();
推荐阅读
- html - 如何从html中的文本框中清除搜索输入?
- python - pandas 将项目添加到一系列列表数据类型
- .net - 如何使用 SOAP Webservice 客户端处理 NTLM 身份验证?
- php - 重新排列 $_FILES 的元素时我的代码出了点问题
- java - 数据截断:不正确的日期时间值:org.joda.time.DateTime
- ios - 使用 Node.js 列出 Firebase iOS 应用中的所有用户
- ios - 如何修复“不变违规:requireNativeComponent:在 UIManager 中找不到“RCTFBLoginButton”。”?
- python - 如何在我的数据框中创建一个新变量,用数据框名称填充值?
- ms-access - 访问:用 2 个点递增/递减一个数字
- couchbase - 过滤器上的 Couchbase 二级索引