【问题标题】:How to get the meta value of the ACF field (wordpress)?如何获取 ACF 字段的元值(wordpress)?
【发布时间】:2021-08-28 02:13:37
【问题描述】:

我需要获取 ACF 字段的元值。
名为“submitdate”的 ACF 字段(格式 => 日期时间选择器:Y-m-d H:i:s)已经有数据“2021-06-11 17:23:36”
我尝试了以下代码,但它只显示正确的 $post->ID,它不显示 $submitdate。
控制台没有错误。
请告诉我如何回显该字段的值?

我试过的查询:

$args = array(
'post_type'         => 'project',
'posts_per_page'    => -1,
'author'            => get_current_user_id(),
'name'              => get_the_title()
    );
$query = new WP_Query($args);

    if ($query->have_posts()) {
    global $post;
    while ($query->have_posts()) {
    $query->the_post();
    $submitdate = get_post_meta($post->ID, 'submitdate', true);
echo $post->ID;
echo $submitdate; 
  }
}

我也试过,它显示当前日期和时间,而不是字段中的值:

$submitdate = get_field("submitdate", $post->ID);



谢谢。

【问题讨论】:

    标签: php mysql wordpress advanced-custom-fields


    【解决方案1】:
    /* I had same issue before few days and resolved with below function */
    
    /* please try this line of code */
        
      $args = array(
        'post_type'         => 'project',
        'posts_per_page'    => -1,
        'author'            => get_current_user_id(),
        'name'              => get_the_title()
       );
      $query = new WP_Query($args);
    
        if ($query->have_posts()) {
        global $post;
       
        while ($query->have_posts()) {
             $query->the_post();
             $submitdate = get_field('submitdate', $post->ID ); // if changed field name then update key in this query
    
              echo $post->ID;
              echo $submitdate; 
         }
       }
    

    【讨论】:

    • 您好:感谢您的帮助。我试过了,但它显示的是当前日期和时间,而不是字段中的值。你能告诉我如何解决这个问题吗?谢谢。
    • 可能是变量冲突。您可以更改变量名称并尝试
    • 你确定吗?您的密钥和帖子 ID 是否正确?
    • 您好,我更改了字段名称(ACF 中的唯一名称),但仍然显示当前时间......
    • 您好 wpdevloper_j:非常感谢您的支持帮助。我喜欢奇怪的东西。我为名为“submitdate”的 ACF 字段设置了日期时间选择器:Y-m-d H:i:s。我用 $value_date = current_datetime();保存帖子时自动保存值。我发现该领域的时间发生了变化。它保持当前时间...您的代码运行良好,我认为只是该字段有问题...
    猜你喜欢
    • 1970-01-01
    • 2018-08-21
    • 2014-06-10
    • 2019-06-03
    • 2021-08-25
    • 2020-06-21
    • 2022-07-07
    • 2017-08-18
    • 1970-01-01
    相关资源
    最近更新 更多