Support

Account

Forum Replies Created

  • I just ran in this this issue as well. A note about this Gutenberg issue in the documentation would be great: https://www.advancedcustomfields.com/resources/post-object/

  • Here’s a solution I’ve come up with, that works with WordPress 5.4 and ACF Pro 5.8.9.

    First you need this function somewhere in functions.php:

    /**
     * Get ID of the first ACF block on the page
     */
    function sg_get_first_block_id() {
        $post = get_post(); 
    
        if(has_blocks($post->post_content)) {
            $blocks = parse_blocks($post->post_content);
            $first_block_attrs = $blocks[0]['attrs'];
    
            if(array_key_exists('id', $first_block_attrs)) {
                return $first_block_attrs['id'];
            }
        }
    }

    This function is used to get the block id of the first block on the page, if this block has a block id. (As far as I can see only ACF blocks have a block id attribute. Core blocks do not).

    Now in your block template file you can do this:

    <?php if($block['id'] === sg_get_first_block_id()): ?>
        <h1><?php the_title(); ?></h1>
    <?php endif; ?>

    $block['id'] is the unique id of every block instance. We then compare it with the id of the first block on the page. If they’re the same, the function will return true, and the title of the post will be displayed.

    How i use it
    I have created a custom cover block with a full size background image and content inside. I wan’t that block to act as a hero element if it’s the very first block on a page.

    By using the above method in my block template file, I can display the custom cover block title in a h1 tag instead of h2, if it’s the first block on the page.

    <?php if($block['id'] === sg_get_first_block_id()): ?>
        <h1><?php echo $title; ?></h1>
    <?php else: ?>
        <h2><?php echo $title; ?></h2>
    <?php endif; ?>

    I also wan’t to hide the ordinary title and breadcrumb, so that the cover block aligns perfectly to the navigation header. For that I have created this function in my functions.php file:

    /**
     * Check first block type
     * @param string block_handle - handle of the block
     */
    function sg_first_block_is($block_handle) {
        $post = get_post(); 
    
        if(has_blocks($post->post_content)) {
            $blocks = parse_blocks($post->post_content);
    
            if($blocks[0]['blockName'] === $block_handle) {
                return true;
            }else {
                return false;
            }
        }
    }

    I can use this as a conditional to check what block type the first block is, and then display or hide the breadcrumb and title accordingly:

    if(!sg_first_block_is('acf/sg-cover')):
        get_template_part('template-parts/components/component', 'breadcrumbs'); 
        echo '<h1>' . get_the_title() . '</h1>';
    endif;
  • The problem continues for me as well. I have added those filters mentioned by briandoh, and I have tried different priorities and replacing esc_html__() with __(), but nothing is being translated. Could the reason be, that I’m trying to do this within a plugin?

    This is my code for loading the translation:

    /**
     * Load translations
     */ 
    add_action('plugins_loaded', 'sg_slide_in_load_plugin_textdomain'); 
    function sg_slide_in_load_plugin_textdomain() {
        load_plugin_textdomain('sg-slide-ins', FALSE, basename(dirname( __FILE__ )) . '/languages/');
    }
    
    add_filter('acf/settings/l10n', 'sg_slide_in_acf_settings_localization');
    function sg_slide_in_acf_settings_localization($localization){
    	return true;
    }
    
    add_filter('acf/settings/l10n_textdomain', 'sg_slide_in_acf_settings_textdomain');
    function sg_slide_in_acf_settings_textdomain($domain){
    	return 'sg-slide-ins';
    }
  • Same here. I haven’t found a solution yet.

    Looks like I’ll have to add it manually. It would be pretty powerful though, to add ACF fields to any WooCommerce email template easily.

  • Thanks @emaildano

    Great solution – just what I needed.

    I would prefer a cleaner way though like @kyle is mentioning though. Maybe getting the meta_id for the repeater fields? That number is unique and short.

  • Will the removal of the metabox cause any problems?

    I code all my themes using only ACF, so it’s no problem for pages and posts, but what about WooCommerce? Isn’t WooCommerce using the built in metabox?

    What are the downsides, if any, for disabling it by default?

Viewing 8 posts - 1 through 8 (of 8 total)