advanced custom field not return back result

大憨熊 提交于 2019-12-10 12:33:33

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/45325903/advanced-custom-field-not-return-back-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!