[Support request] Container not wrapping content from shortcode

Home Forums Support [Support request] Container not wrapping content from shortcode

Home Forums Support Container not wrapping content from shortcode

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #2333792
    Derek

    Hi there,

    Coming from Elementor via Oxygen I’m finding my way round GP and GB with a very simple test site. I’m impressed so far and have managed to solve a bunch of things based on excellent documentation and previous forum topics.

    I hope you can help me resolve one issue. The single page element I’ve created for a custom post type (“events”) includes a right sidebar element with two shortcodes that fetch ACF fields. I’ve used Code Snippets to hide headings if the assocociated ACF field is empty.

    add_shortcode('custom_event_cost',function(){
    $event_cost_field = get_field( "cost", get_the_ID() );
    if( !empty($event_cost_field) ){ //if value exists
    echo '<p><h4>Cost </h4>'.$event_cost_field.'</p>';
    } 
    });
    
    add_shortcode('custom_event_date',function(){
    $event_date_field = get_field( "date", get_the_ID() );
    if( !empty($event_date_field) ){ //if value exists
    echo '<p><h4>Date </h4>'.$event_date_field.'</p>';
    } 
    });

    So far so good.

    I’ve wrapped the shortcode blocks in a container and applied some simple styling to it. But the container is displying below the output of the shortcode:

    http://blocks.lunadev.co.uk/events/winslade-wednesday/

    Can you help me understand why this is and how I can fix it?

    Assuming this is possible, can you help me with PHP for a new code snippet to hide the container (or the sidebar) if the ACF fields are both empty?

    Thank you!

    #2333838
    David
    Staff
    Customer Support

    Hi there,

    shortcodes should only return their content, so when you echo it gets output to the content.
    You can use object buffering to fix that – see here:

    https://docs.generatepress.com/article/creating-a-shortcode/

    However if you use GenerateBlocks, you can use its built in Dynamic Data options to display simple custom field values. See here for example:

    https://docs.generateblocks.com/article/headline-overview/#dynamic-data

    And if instead of using a Widget Area you use a Block Element eg. a hook or a sidebar:

    https://docs.generatepress.com/article/block-element-sidebar/

    Then the GB Container Block inside a Block Element has the Remove container condition and you can set that to No Post Meta

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.