【问题标题】:Get values from most recent post从最近的帖子中获取值
【发布时间】:2019-08-10 07:01:32
【问题描述】:

我最近的帖子中有通过高级自定义字段放置的数字值。我希望能够将数据从帖子中提取到另一个页面。这可以通过 ID 轻松完成: https://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/ 但我无法完成的是从最近的帖子中获得这种吸引力。 ACF 支持网站没有提及最新的。

     <?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();
     ?>

【问题讨论】:

  • 由于您只获得最近的一篇文章,因此您可能不需要 foreach 循环。你需要的是post ID,对吧?那么当你var_dump($recent_posts); 时会发生什么?
  • 哦,是的,我想我知道你在做什么。我可以拉出最新的帖子,获取 id 并将其设置为变量,然后在设置 acf 查询时使用该变量。我会尝试一下并报告回来。如果这有助于我解决我的问题,我们可以改写它并给你信用。不管怎样,谢谢,我有一个新的方向可以尝试。

标签: wordpress advanced-custom-fields acfpro


【解决方案1】:

来自 https://developer.wordpress.org/reference/functions/wp_get_recent_posts/ 上的 Codex(稍作修改):

<?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(); ?>

【讨论】:

  • 这行得通。我不得不根据自己的需要稍微修改一下,但逻辑是 100%。正是我想要的。谢谢@jarom
  • 很高兴它有帮助!
猜你喜欢
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多