advanced custom field not return back result

2019-06-14 22:23发布

i am using advanced custom filed and i made custom author field (it could be Publisher Or Brand etc) now this author's name is not printing on product (Book) page . in custom field its for author's name slug is 'names'

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;
      }

    }

for this i got the result Check this report screenshot . now problem is to show the Authors name which is ['post_title'] in this array. i tried so many solutions but not working its not showing the result. i used to show this result by this code

echo the_field('names');

'names' is the field name in 'Authors' custom field.

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-14 22:52

You may use one of the methods below . You have to strictly set $post otherwise get_the_ID() funtion return false.

    global = $post;
    $custom_key = 'author';
    $post_id = get_the_ID();



    echo $customer_key_value = get_post_meta( $post_id, $custom_key , true );

OR

    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 <br />"; 
    }
查看更多
啃猪蹄的小仙女
3楼-- · 2019-06-14 22:58

try this code for ACF

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

fetch the post title

<?php echo get_the_title(); ?>

for display author name for below function

<?php echo get_the_author(); ?>
查看更多
登录 后发表回答