Php Wordpress,查询后更改全局帖子
我正在开发一个插件。在这个插件中,我创建了一个自定义的post类型“toto” 在管理页面中,我编辑了一个自定义的post元素,其post id为'82'。 在本页中,我启动了一个查询,以检索具有另一种post_类型的元素,如下所示:Php Wordpress,查询后更改全局帖子,php,wordpress,Php,Wordpress,我正在开发一个插件。在这个插件中,我创建了一个自定义的post类型“toto” 在管理页面中,我编辑了一个自定义的post元素,其post id为'82'。 在本页中,我启动了一个查询,以检索具有另一种post_类型的元素,如下所示: $featured_args = array( 'post_type' => 'other_type', 'post_status' => 'publish' ); // The Featured Posts query. $featured
$featured_args = array(
'post_type' => 'other_type',
'post_status' => 'publish'
);
// The Featured Posts query.
$featured = new WP_Query( $featured_args );
// Proceed only if published posts with thumbnails exist
if ( $featured->have_posts() ) {
while ( $featured->have_posts() ) {
$featured->the_post();
if ( has_post_thumbnail( $featured->post->ID ) ) {
/// do stuff here
}
}
// Reset the post data
wp_reset_postdata();
}
这样一来,全局$post就改变了。它不再是id为82的post,而是查询中最新的post元素
我认为wp_reset_postdata()函数将允许我检索当前的$post。我还尝试了wp_reset_query(),没有做任何更改。
我错过什么了吗
任何帮助都将不胜感激。wp\u reset\u postdata()
重置主循环。如果您想直接访问$post
,请尝试
global $post;
$backup_post = $post;
//do another loop
wp_reset_postdata();
$post = $backup_post;
wp\u reset\u postdata()
重置主循环。如果您想直接访问$post
,请尝试
global $post;
$backup_post = $post;
//do another loop
wp_reset_postdata();
$post = $backup_post;
这是我正在考虑的解决办法。谢谢Max。我将使用此解决方案。这是我正在考虑的解决方案。谢谢Max。我将使用此解决方案。