Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1289880
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI built a custom block using ACF acf_register_block_type();
function
it works fine in the WordPress dashboard (block editor), it shows the field data and all good..
but in the front-end (view page) the fields are NULL
any help on how to display field data in the front-end??
functions.php code:
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 code:
<?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, 74, 122, 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'));
Note: I'm using WordPress v5.7.2 and ACF PRO v5.9.8
本文标签: ACF custom block getfield() shows null on frontend
版权声明:本文标题:ACF custom block get_field() shows null on front-end? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741434501a2378555.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论