【问题标题】:How to access advanced custom field values within a gutenberg block?如何访问古腾堡块中的高级自定义字段值?
【发布时间】:2019-09-25 12:32:05
【问题描述】:

我有一个带有一些高级自定义字段的自定义帖子类型。我正在尝试从 Gutenberg 块中访问这些自定义字段值。

我已将以下内容添加到我的 register_post_type 函数中

    'show_in_rest' => true,
    'supports' => array( 'title', 'editor', 'custom-fields' ),

我可以使用以下方法从我的 Gutenberg 块中成功检索自定义帖子:

select('core').getEntityRecords('postType', 'customType')

但我没有看到自定义字段或其值。

这是我的块的 JavaScript:

const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const { withSelect } = wp.data;

registerBlockType('cgb/block-press-block', {
  title: __('Press Block'),
  icon: 'awards',
  category: 'common',
  keywords: [
    __('press-block'),
  ],
  edit: withSelect((select) => {
    return {
      posts: select('core').getEntityRecords('postType', 'press')
    };
  })(({posts}) => {
    return <p>Content</p>;
  }),
});

有没有办法在编辑器端访问自定义帖子的高级字段值或将这些数据传递到块?

【问题讨论】:

    标签: wordpress advanced-custom-fields wordpress-gutenberg gutenberg-blocks


    【解决方案1】:

    由于您已经在使用高级自定义字段,您是否可以使用acf_register_block 而不是独立注册自己的块?这样您就可以在基于 PHP 的模板中从 ACF 访问字段。

    这里有一些有用的链接:

    此代码取自上面的 ACF 博客文章,并在此处发布以确保上述链接更改的完整性。

    注册 ACF 块:

    add_action('acf/init', 'my_acf_init');
    function my_acf_init() {
    
        // check function exists
        if( function_exists('acf_register_block') ) {
    
            // register a testimonial block
            acf_register_block(array(
                'name'              => 'testimonial',
                'title'             => __('Testimonial'),
                'description'       => __('A custom testimonial block.'),
                'render_callback'   => 'my_acf_block_render_callback',
                'category'          => 'formatting',
                'icon'              => 'admin-comments',
                'keywords'          => array( 'testimonial', 'quote' ),
            ));
        }
    }
    

    包含块模板的回调函数:

    function my_acf_block_render_callback( $block ) {
    
        // convert name ("acf/testimonial") into path friendly slug ("testimonial")
        $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/block/content-{$slug}.php") ) ) {
            include( get_theme_file_path("/template-parts/block/content-{$slug}.php") );
        }
    }
    

    块的 HTML:

    <?php
    /**
     * Block Name: Testimonial
     *
     * This is the template that displays the testimonial block.
     */
    
    // get image field (array)
    $avatar = get_field('avatar');
    
    // create id attribute for specific styling
    $id = 'testimonial-' . $block['id'];
    
    // create align class ("alignwide") from block setting ("wide")
    $align_class = $block['align'] ? 'align' . $block['align'] : '';
    
    ?>
    <blockquote id="<?php echo $id; ?>" class="testimonial <?php echo $align_class; ?>">
        <p><?php the_field('testimonial'); ?></p>
        <cite>
            <img src="<?php echo $avatar['url']; ?>" alt="<?php echo $avatar['alt']; ?>" />
            <span><?php the_field('author'); ?></span>
        </cite>
    </blockquote>
    <style type="text/css">
        #<?php echo $id; ?> {
            background: <?php the_field('background_color'); ?>;
            color: <?php the_field('text_color'); ?>;
        }
    </style>
    

    这将创建一个基本的推荐块作为一个简单的起点。 ACF 在 Gutenberg 中处理 JavaScript 处理,因此您所要做的就是担心 PHP 方面的事情。

    这意味着您可以像我们(ACF 粉丝)一样使用 get_field()the_field() 函数。在不使用这种原生方式的情况下混合 ACF 和 Gutenberg 可能会让人头疼,并且可能需要插件才能通过 WordPress REST API 访问字段。

    注意:对 Gutenberg 块的 ACF 支持需要 ACF 5.8 或更高版本。

    【讨论】:

    • 所以我使用create-guten-block,它允许我在一个插件中创建多个块。我希望避免创建一个单独的插件。有没有办法可以将此功能添加到我当前的插件中?
    • 是的,如果您愿意,您可以在自己的插件中使用 ACF 块功能。
    • 我没有意识到 ACF 5.8 仍处于测试阶段。我会看看我是否可以在我当前的配置中升级和使用acf_register_block。如果没有,我可能会放弃使用 Gutenberg 块,而只是在模板中呈现自定义帖子内容。
    • 我最终使用了这个功能,并且能够轻松检索我需要的所有自定义帖子类型数据。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 2022-10-20
    • 2019-05-08
    • 1970-01-01
    • 2021-10-08
    • 2019-01-23
    相关资源
    最近更新 更多