我目前正在研究一个主题,我已经建立了一个帖子模板。
此帖子模板链接到一些自定义帖子类型。
当我在实际的帖子模板上查询我的帖子类型时,会因为某种原因使内容消失?这里有我遗漏的东西吗?
谢谢Mark
我的循环如下:
<?php
$query = \'posts_per_page=10&post_type=articles\';
$queryObject = new WP_Query($query);
// The Loop...
if ($queryObject->have_posts()) {
while ($queryObject->have_posts()) {
$queryObject->the_post(); the_title(); the_content();
}
}
?>
SO网友:Johannes Pille
为了完整性,在使用wp_reset_query()
不一定是错误的,如果在具有WP_Query
班这个$wp_query
对象未更改,因此无需重置。只有$post
全局需要重置。因此wp_reset_postdata()
在这种情况下就足够了。
看见/wp-includes/query.php 供参考:
/**
* Destroy the previous query and set up a new query.
*
* This should be used after {@link query_posts()} and before another {@link
* query_posts()}. This will remove obscure bugs that occur when the previous
* wp_query object is not destroyed properly before another is set up.
*
* @since 2.3.0
* @uses $wp_query
*/
function wp_reset_query() {
unset($GLOBALS[\'wp_query\']);
$GLOBALS[\'wp_query\'] = $GLOBALS[\'wp_the_query\'];
wp_reset_postdata();
}
/**
* After looping through a separate query, this function restores
* the $post global to the current post in the main query
*
* @since 3.0.0
* @uses $wp_query
*/
function wp_reset_postdata() {
global $wp_query;
if ( !empty($wp_query->post) ) {
$GLOBALS[\'post\'] = $wp_query->post;
setup_postdata($wp_query->post);
}
}