Support

Account

Home Forums Front-end Issues PHP code for ACF inside WordPress add_filter

Helping

PHP code for ACF inside WordPress add_filter

  • Hello everyone!

    I have a problem with my site, i need to insert a field at a specific point on my WordPress website and the developer of the theme gave me this snippet for it:

    add_filter(
    'hivepress/v1/templates/vendor_view_page/blocks',
    function( $blocks, $template ) {
        return hivepress()->helper->merge_trees(
                [ 'blocks' => $blocks ],
                [
                    'blocks' => [
                        'page_content' => [
                            'blocks' => [
                                'custom_listings_text' => [
                                        'type'    => 'content',
                                        'content' => 'Your content here',
                                        '_order'  => 1,
                                ],
                            ],
                        ],
                    ],
                ]
            )['blocks'];
    },
    1000,
    2
    );

    It works perfect, but my problem is that i can’t get my head around how i change the ‘Your content here’ so i get the information of my ACF.
    I know that the “normal” code is:

    <?php if( get_field('text_field') ): ?>
    <h2><?php the_field('text_field'); ?></h2>
    <?php endif; ?>

    But how do I “insert” this into the snippet?
    I’m sorry if this isn’t the right forum or if there’s a really simple solution to this, but as a complete beginner I’ve been dealing with this problem for days and I can’t find a solution.

  • 
    add_filter(
    'hivepress/v1/templates/vendor_view_page/blocks',
    function( $blocks, $template ) {
    if (get_field('text_field')) {
      $content = get_field('text_field');
        return hivepress()->helper->merge_trees(
                [ 'blocks' => $blocks ],
                [
                    'blocks' => [
                        'page_content' => [
                            'blocks' => [
                                'custom_listings_text' => [
                                        'type'    => 'content',
                                        'content' => $content,
                                        '_order'  => 1,
                                ],
                            ],
                        ],
                    ],
                ]
            )['blocks'];
    } // end if
    },
    1000,
    2
    );
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.