Php Wordpress,安装程序\u postdata()未完成其工作
我的帖子按日期排序,日期由高级自定义字段日期选择器选择。我想使用常规的Wordpress函数引用[u title,etc…]和post相关的自定义字段。 现在每个循环的输出都是相同的。我读过setup\u postdata可以解决这个问题,并支持使用常规函数引用。我试图应用它,但输出总是一样的。谢谢Php Wordpress,安装程序\u postdata()未完成其工作,php,wordpress,global,Php,Wordpress,Global,我的帖子按日期排序,日期由高级自定义字段日期选择器选择。我想使用常规的Wordpress函数引用[u title,etc…]和post相关的自定义字段。 现在每个循环的输出都是相同的。我读过setup\u postdata可以解决这个问题,并支持使用常规函数引用。我试图应用它,但输出总是一样的。谢谢 <?php global $posts; $posts = get_posts(array( 'post_type' => 'post', 'meta_key' =
<?php
global $posts;
$posts = get_posts(array(
'post_type' => 'post',
'meta_key' => 'release_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
$group_posts = array();
if( $posts ) {
foreach( $posts as $post ) {
$date = get_field('release_date', $post->ID, false);
$date = new DateTime($date);
$year = $date->format('Y');
$month = $date->format('F');
$group_posts[$year][$month][] = array($post, $date);
}
}
foreach ($group_posts as $yearKey => $years) {
foreach ($years as $monthKey => $months) {
echo '<li class="time">' . $monthKey . ' ' . $yearKey . '</li>';
foreach ($months as $postKey => $posts) {
setup_postdata($posts); ?>
<li class="item clearfix">
<!-- Wordpress Functions -->
<?php the_title();?>
<?php the_permalink();?>
<!-- Advanced Custom Fields -->
<?php the_field('blabla')?>
</li>
<?php
}
}
} wp_reset_postdata();
?>
您只需要添加一行$post=$current\u post;就在调用setup_postdata$post之前。请参见下面的示例,以获得清晰的想法:
$posts = get_posts(array(.......));
// Call global $post variable
global $post;
// Loop through sorted posts and display using template tags
foreach( $posts as $current_post ) :
//the below line is what you missed!
$post = $current_post; // Set $post global variable to the current post object
setup_postdata( $post ); // Set up "environment" for template tags
// Use template tags normally
the_title();
the_post_thumbnail( 'featured-image-tiny' );
the_excerpt();
endforeach;
wp_reset_postdata();
有关详细信息,请参见《WP法典》中发布的 只需添加一行$post=$current\u post;就在调用setup_postdata$post之前。请参见下面的示例,以获得清晰的想法:
$posts = get_posts(array(.......));
// Call global $post variable
global $post;
// Loop through sorted posts and display using template tags
foreach( $posts as $current_post ) :
//the below line is what you missed!
$post = $current_post; // Set $post global variable to the current post object
setup_postdata( $post ); // Set up "environment" for template tags
// Use template tags normally
the_title();
the_post_thumbnail( 'featured-image-tiny' );
the_excerpt();
endforeach;
wp_reset_postdata();
有关详细信息,请参见《WP法典》中发布的 设置\u postdata$post;这应该在使用函数之前完成。在此循环之后,每个$months的$postKey=>$posts{?>请检查我更新的原始帖子。也许我误解了你,但结果仍然没有改变。感谢你的回答删除全局$post;在设置之前\u postdata$post;嗯,很抱歉,它仍然不会改变循环的结果。一直都是相同的标题,permalink等。但再次感谢好的,你有打字错误,请检查它是否有效setup_postdata$posts;而不是setup_postdata$post;setup_postdata$post;这应该在使用*funcs之前。在每个$months的循环之后,作为$postKey=>$posts{?>请检查我更新的原始帖子。也许我误解了你,但结果仍然没有改变。感谢你的回答删除全局$post;在设置之前\u postdata$post;嗯,很抱歉,它仍然不会改变循环的结果。一直都是相同的标题,permalink等。但再次感谢好的,你有打字错误,请检查它是否有效设置\u postdata$post;而不是设置\u postdata$post;