Wordpress 从最近的帖子中获取值
我最近发布的文章中有一些数值是通过高级自定义字段放置的。我希望能够将文章中的数据拉到另一个页面中。这可以通过ID轻松完成: 但我无法做到的是,从最近的帖子中得到这一点。ACF支持站点未提及最新版本Wordpress 从最近的帖子中获取值,wordpress,advanced-custom-fields,acfpro,Wordpress,Advanced Custom Fields,Acfpro,我最近发布的文章中有一些数值是通过高级自定义字段放置的。我希望能够将文章中的数据拉到另一个页面中。这可以通过ID轻松完成: 但我无法做到的是,从最近的帖子中得到这一点。ACF支持站点未提及最新版本 <?php $args = array( 'numberposts' => '1' ); $recent_posts = wp_get_recent_posts( $args ); foreach( $recent_posts as $recen
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
// acf query in here. not included for brevity.
endif;
}
wp_reset_query();
?>
从法典开始,稍作修改:
<?php
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 1, // Number of recent posts thumbnails to display
'post_status' => 'publish' // Show only the published posts
));
foreach($recent_posts as $post) : ?>
<a href="<?php echo get_permalink($post['ID']) ?>">
<?php echo get_the_post_thumbnail($post['ID'], 'full'); ?>
<p class="custom-class"><?php echo $post['post_title'] ?></p>
</a>
<?php
$custom_field = get_field('custom_field_name', $post['ID']);//for ACF fields
?>
<?php endforeach; wp_reset_query(); ?>
因为您只收到最近的一篇文章,所以可能不需要foreach循环。你需要的是邮政ID,对吗?那么,当您var_转储$recents_posts;,会发生什么呢;?哦,是的,我想我明白你的意思了。我可以拉取最近的帖子,获取id并将其设置为变量,然后在设置acf查询时使用该变量。我会尝试一下,然后回来汇报。如果这有助于我解决我的问题,我们可以重新措辞,并给你信用。不管怎样,谢谢,我有一个新的方向要尝试。这很有效。我不得不根据我的需要修改一些东西,但逻辑是100%。正是我想要的。谢谢@Jarom很高兴它起了作用!