0

I've created a theme from scratch and I have issues creating shortcodes. I have the following code:

functions.php

function caption_shortcode( $atts, $content = null ) {
    return '<span class="caption">' . $content . '</span>';
}
add_shortcode( 'caption', 'caption_shortcode' );

in the WP Admin page editor:

 [caption]My Caption[/caption]

on the page template page:

echo do_shortcode('[caption]');

The shortcode seems to be somehow working as it returns the HTML but not the $content.

My problem is that I can't seem to get my hand on the $content and display it using the shortcode. Any idea why this is happening?

P.S. I don't want to use the_content() function to display all the content, I want to use the shortcodes to divide the content the user adds in several pop-ups and child sections of the page.

Thanks!

3
  • 1
    Did you try echo do_shortcode('[caption]My Caption[/caption]');? Commented Mar 29, 2016 at 12:49
  • It works, but I don't want to add the content from the code, I want the user to be able to add and delete it anytime. Commented Mar 29, 2016 at 13:00
  • you either can add the content via editor or the comment provided above . Else you can create a textbox ACF for every content and then write <?php echo do_shortcode('[caption]'.get_field("field_name").'[/caption]');?> Commented Mar 29, 2016 at 13:19

1 Answer 1

-1

Make sure you user shotcode same page

  // [baztag]content[/baztag]
    function baztag_func( $atts, $content = '' ) {
        return $content;
    }
    add_shortcode( 'baztag', 'baztag_func' );

 echo do_shortcode('[baztag]');
Sign up to request clarification or add additional context in comments.

1 Comment

This does not answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.