高级自定义字段无法在自定义页面模板 wordpress 中显示值

Advance Custom Field unable to show values in custom page template wordpress

自定义 "single.php" 无法显示高级自定义字段值。

这是使用自定义 "single.php"

获取自定义 "page template" 页面 ID 的函数
enter code here

<?php
  function lang_page_id($id){
    if(function_exists('icl_object_id')) 
    {
      return icl_object_id($id,'page', false,ICL_LANGUAGE_CODE);
    } else 
    {
     return $id;
    }
   }
?>

这是为了显示acf值

enter code here

<h4 class="x-feature-box-title"><?php the_field('smr_header', 
lang_page_id(355));?>

删除以下内容:

<?php
    function lang_page_id($id){
       if(function_exists('icl_object_id')) 
       {
           return icl_object_id($id,'page', false,ICL_LANGUAGE_CODE);
       } else 
       {
           return $id;
       }
    }
?>

在您的代码中:

<h4 class="x-feature-box-title"><?php the_field('smr_header', 355);?>

或者:

<?php $h4_feature_title = get_field('smr_header', 355);?>
<h4 class="x-feature-box-title"><?php echo $h4_feature_title;?></h4>

ACF 已有 post 个特定值,只需输入您要从中获取字段的 post 的 ID。我不确定你为什么要在当前页面上使用它,因为那时你应该只使用 the_field('smr_header');因为这是从当前页面获取的。

如果您确实想获取页面的 ID,请在循环内:

$postid = get_the_ID();
echo $postid;

并且只包含在自定义页面模板中。

如果您确实想获取页面的 ID,请在循环外使用以下代码创建一个函数:

global $wp_query;
echo $wp_query->post->ID;