admin管理员组

文章数量:1122832

I'm using GridBuilder WP (great plugin, average support) to show a grid of a custom post type. Ultimately, I need to show an ACF taxonomy field, and their support have recommended using one of their nifty Custom Blocks, but at the moment I'm struggling to just show a text field (as a test) in a Custom Block. This is the suggested code, but it always shows the label "Email" with an empty field, even though there is a normal block above showing the custom field successfully.

add_filter(
    'wp_grid_builder/blocks',
    function( $blocks ) {

        $blocks['email_block'] = [
            'name'            => __( 'Email block', 'text-domain' ),
            'render_callback' => function() {

                // Get email from ACF field.
                global $post;
                $post = wpgb_get_post();
                setup_postdata( $post );
                $email = get_field('email');

                if( $email ) {
                    printf(
                        '<a href="mailto:%1$s" rel="external noopener noreferrer" target="_blank">%2$s</a>',
                        esc_attr( rawurlencode( $email ) ),
                        esc_html( 'Email' )
                    );              
                }
                wp_reset_postdata();
                

            },
        ];

        return $blocks;

    }
);

Has anyone managed to show custom fields in a Gridbuilder WP custom block?

I'm using GridBuilder WP (great plugin, average support) to show a grid of a custom post type. Ultimately, I need to show an ACF taxonomy field, and their support have recommended using one of their nifty Custom Blocks, but at the moment I'm struggling to just show a text field (as a test) in a Custom Block. This is the suggested code, but it always shows the label "Email" with an empty field, even though there is a normal block above showing the custom field successfully.

add_filter(
    'wp_grid_builder/blocks',
    function( $blocks ) {

        $blocks['email_block'] = [
            'name'            => __( 'Email block', 'text-domain' ),
            'render_callback' => function() {

                // Get email from ACF field.
                global $post;
                $post = wpgb_get_post();
                setup_postdata( $post );
                $email = get_field('email');

                if( $email ) {
                    printf(
                        '<a href="mailto:%1$s" rel="external noopener noreferrer" target="_blank">%2$s</a>',
                        esc_attr( rawurlencode( $email ) ),
                        esc_html( 'Email' )
                    );              
                }
                wp_reset_postdata();
                

            },
        ];

        return $blocks;

    }
);

Has anyone managed to show custom fields in a Gridbuilder WP custom block?

Share Improve this question asked Mar 9, 2022 at 21:27 clayRayclayRay 1351 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Not sure if you have already resolved your issue, but I am in a similar situation. You could take a look at this webpage for guidance:

https://wpdevdesign.com/how-to-create-custom-blocks-in-wp-grid-builder/

本文标签: filtersHow to show ACF fields in Gridbuilder custom block