首页 文章

高级自定义字段不返回结果

提问于
浏览
-1

我正在使用高级自定义文件和我制作自定义作者字段(它可能是出版商或品牌等)现在这个作者的名字不打印在产品(书)页面上 . 在自定义字段中,其作者姓名slug是'名字'

add_action( 'woocommerce_after_single_product_summary', "ACF_product_content", 10 );

    function ACF_product_content(){

      echo '<h2> ACF Content </h2>';

      if (function_exists('the_field')){
        echo '<p>Woohoo, the_field function exists! </p>';

        //the_field('authors');

        echo '<pre>';
        print_r(get_the_ID());
        echo '</pre>';
        echo '<pre>';
            print_r(get_field('authors'));
        echo '</pre>';

        die;
      }

    }

为此,我得到了结果Check this report screenshot . 现在的问题是在这个数组中显示作者姓名['post_title'] . 我尝试了很多解决方案,但没有工作,没有显示结果 . 我曾经用这段代码显示这个结果

echo the_field('names');

'names'是'作者'自定义字段中的字段名称 .

2 回答

  • 1

    尝试使用此代码进行ACF

    <?php
    echo get_field('your custom filed slug name',get_the_ID());
    ?>
    

    获取帖子 Headers

    <?php echo get_the_title(); ?>
    

    用于显示以下功能的作者姓名

    <?php echo get_the_author(); ?>
    
  • 0

    您可以使用以下方法之一 . 你必须严格设置$ post否则 get_the_ID() 功能返回 false .

    global = $post;
        $custom_key = 'author';
        $post_id = get_the_ID();
    
    
    
        echo $customer_key_value = get_post_meta( $post_id, $custom_key , true );
    

    要么

    global = $post;
        $custom_key = 'author';
        $post_id = get_the_ID();
    
        $custom_values = get_post_custom_values( $custom_key , $post_id );
    
        foreach ( $custom_values as $key => $value ) {
           echo "$key  => $value 
    "; }

相关问题