微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ACF 自定义块 get_field() 在前端显示为空?

如何解决ACF 自定义块 get_field() 在前端显示为空?

我使用 ACF acf_register_block_type(); 函数构建了一个自定义块 它在 wordpress 仪表板(块编辑器)中运行良好,它显示了字段数据,一切都很好。 但在前端(查看页面)字段为 NULL 关于如何在前端显示字段数据有帮助吗??

functions.PHP 代码

add_action('acf/init','my_acf_init_block_types');
function my_acf_init_block_types()
{
    // Check function exists.
    if (function_exists('acf_register_block_type')) {
        // register a hero block.
        acf_register_block_type(array(
            'name'              => 'banner','title'             => __('Banner'),'description'       => __('A custom hero block.'),'render_callback'   => 'my_acf_block_render_callback','category'          => 'home-sections','icon'              => 'cover-image','keywords'          => array('basic','banner')
        ));
    }
}

function my_acf_block_render_callback($block)
{
    // convert name ("acf/hero") into path friendly slug ("hero")
    $slug = str_replace('acf/','',$block['name']);
    // include a template part from within the "template-parts/block" folder
    if (file_exists(get_theme_file_path("/template-parts/blocks/content-{$slug}.PHP"))) {
        include(get_theme_file_path("/template-parts/blocks/content-{$slug}.PHP"));
    }
}


function wpdocs_mytheme_block_styles()
{
    wp_enqueue_style('admin-css',get_stylesheet_directory_uri() . '/build/css/styles.min.css','0.0.3');
    wp_enqueue_script('admin-js',get_template_directory_uri() . '/build/js/scripts.min.js',array('jquery'),'0.0.3',true);
} 
add_action('enqueue_block_editor_assets','wpdocs_mytheme_block_styles')

banner.PHP 代码

<?PHP
  $title = get_field('main_title');
    $img = get_field('background_image');
    $button = get_field('button');
?>
<section id="banner-section" style=" background-image:linear-gradient( rgba(67,74,122,0.5),rgba(67,0.5)  ),url('<?= $img['url']; ?>');">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-12 col-lg-10 text-center">
                <h1 class="basic-slider-heading"> <?PHP echo $title ?> </h1>
                <?PHP
                if ($button) :
                    $button_url = $button['url'];
                    $button_title = $button['title'];
                    $button_target = $button['target'] ? $button['target'] : '_self';
                ?>
                    <button class="btn btn-shadow-malibu btn-bg-malibu-gradient bg-malibu-accent">
                        <a href="<?= $button_url ?>" target="<?= $button_target; ?>"><?= $button_title ?></a>
                    </button>
                <?PHP endif; ?>
            </div>
        </div>
    </div>
</section>

var_dump( get_field('main_title'));

enter image description here

注意:我使用的是 wordpress v5.7.2 和 ACF PRO v5.9.8

解决方法

这是 get_field 的文档:https://www.advancedcustomfields.com/resources/get_field/

注意有一个可选的第二个参数

$post_id(混合)(可选)保存值的帖子 ID。默认为当前帖子。

因此,如果您的函数在一个地方工作而不是在另一个地方工作,那么这两个页面之间存在环境差异。有可能在 UI 上没有技术上选择帖子,在这种情况下,您需要传递您打算获取其字段的帖子的 id。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?