TransWikia.com

Creating page and post specific ACF Gutenberg blocks

WordPress Development Asked by Freddy on February 5, 2021

I’m creating custom ACF Gutenberg blocks my a site and have successfully managed to register my own blocks. Now, I have a custom post type called Blog. I do not want blog to show all my ACF Gutenberg blocks, I want to create a separate batch for custom post type use only. I have enabled show_in_rest, but even the default Gutenberg blogs do not show for me?

Here is my approach:

1. Registering the post type (theme/functions.php)

<?php
register_post_type('Blog', theme_build_post_args('Blog', 'Blog', 'Blog', array(
    'show_in_rest' => true,
    'menu_icon' => 'dashicons-edit',
    'menu_position' => 20,
    'has_archive' => true,
    'public' => true,
    'supports' => array(
        'editor',
        'title',
        'author',
        'revisions',
        'excerpt',
        'thumbnail'
    ) ,
)));

?>

2. Registering the ACF Gutenberg blocks for pages (theme/inc/acf-blocks/blocks.php)

Here are the blocks that I’ve registered for use on pages (not on the blog post type):

<?php
$hero = array(
    'name' => 'hero',
    'title' => __('Hero') ,
    'description' => __('') ,
    'render_callback' => 'block_render',
    'category' => 'formatting',
    'icon' => 'admin-comments',
    'keywords' => array(
        'hero'
    ) ,
);

$blocks = [$hero];

return $blocks;

?>
  1. Registering the ACF Gutenberg blocks for blog post type (theme/inc/acf-blocks/blog-blocks.php)
<?php
$blog_hero = array(
    'name' => 'blog_hero',
    'title' => __('Blog hero') ,
    'description' => __('') ,
    'render_callback' => 'block_render',
    'category' => 'formatting',
    'icon' => 'admin-comments',
    'keywords' => array(
        'hero',
        'blog'
    ) ,
);

$blog_blocks = [$blog_hero];

return $blog_blocks;

?>
  1. Register all blocks (theme/inc/acf-blocks/functions.php)
<?php

/*
* loop though array and register each block type
*/

function block_acf_init(){
  $path = get_template_directory().'/inc/acf-blocks/blocks.php';
  $blocks = require($path);
  foreach($blocks as $block) {
    acf_register_block_type($block);
  }
}

function blog_acf_init(){
  $path = get_template_directory().'/inc/acf-blocks/blog-blocks.php';
  $blog_blocks = require($path);
  foreach($blog_blocks as $blog_block) {
    acf_register_block_type($blog_block);
  }
}


// Check if function exists, and hook into setup
if( function_exists('acf_register_block_type') ) {
  add_action('acf/init', 'block_acf_init');
  add_action('acf/init', 'blog_acf_init');
}


?>

Current results:

When creating a post on the blog custom post type, I do not have the ability to add any blocks, let alone see if blog_hero block appears:

enter image description here

On pages, I can see all my created blocks, however, the blog hero block shows on the page side, when I only want it for the custom post type:

enter image description here

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP