Question

I have a two part shortcode, with opening and closing shortcodes, in a template file. See example below.

echo do_shortcode('[myshortcode][/myshortcode]');

I see from the Codex that I can do this:

echo do_shortcode('[myshortcode]' . '<div class="anyHTML">Any Text</div>' . '[/myshortcode]');

Is it possible/correct or will it work to do this instead?

echo do_shortcode('[myshortcode]');
    $somePHP = possibleWordPressLoopCodeOrOtherPHP;
echo do_shortcode('[/myshortcode]');

I'm wondering about this to see if I can include PHP inside a two part shortcode, possibly a WordPress loop for a CPT or even other PHP code.

Était-ce utile?

La solution

You need to have the whole shortcode block inside the do_shortcode function. So you could do something like this:

$text = some_code_or_function_that_returns_text_your_shortcode_can_act_on();
$sc_string = sprintf("[myshortcode]\n%s\n[/myshortcode]", $text);
echo do_shortcode($sc_string);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top