Acf插件的价值不在于wordpress中的the_field

时间:2017-11-03 05:28:51

标签: wordpress widget advanced-custom-fields shortcode

enter image description here

我希望使用短代码显示acf值,我在function.php文件中有函数,并在widget侧边栏中添加了这个短代码,但它没有返回值。在图像文件中我将图像返回值设置为图像url但它不起作用。

1)我在function.php文件中编写的代码

function myfunction() { 
?>

    <img src="<?php the_field('pop_img'); ?>" alt="no image" />

    <p><?php the_field('pop_date'); ?></p>

    <p><?php the_field('pop_btn_text'); ?></p>

<?php }
add_shortcode('display_popup','myfunction');

2)我进入小部件的输出只是

 "no image"

3)在显示带有post id的值时,它会返回类似这样的内容

code: 
<?php 
        echo '<pre>';
            print_r( get_fields(8647) );
        echo '</pre>';
        // die; 
    ?>

Output:
Array
(
    [] => 
    [pop_img] => http://localhost/wordpress/wp-content/uploads/2017/10/josh-woodward-gravity-mp3-image.jpg
    [pop_date] => 20171105
    [pop_btn_text] => dsdfdscfvdsvdv
)

1 个答案:

答案 0 :(得分:2)

使用get_field()函数获取ACF值

 <?php 
    function myfunction() { 
       $popup_field_array= get_fields(8647);
        //$pop_img_path = get_field('pop_img', get_the_ID());
        $pop_img_path = $popup_field_array['pop_img'];
        $pop_date = $popup_field_array['pop_date'];
        $pop_btn_text = $popup_field_array['pop_btn_text'];
        if($pop_img_path != "")
        {
            ?>
                <img src="<?php echo $pop_img_path; ?>" alt="no image" />
            <?php
        }
    ?>
       <?php /*?> <p><?php echo get_field('pop_date', get_the_ID()); ?></p>
        <p><?php echo get_field('pop_btn_text',get_the_ID()); ?></p><?php */?>
         <p><?php echo $pop_date; ?></p>
        <p><?php echo $pop_btn_text; ?></p>
    <?php 
    }
    add_shortcode('display_popup','myfunction');
?>