0

So I've written a shortcode to generate a Loop (which shows the most recent posts), the shortcode is simply [loop]. Here's the code which outputs the content of each post (which is the bit causing me problems):

$output .= "<p class='entry-content'>" . get_the_content() . "</p></div>";

So I write posts and the shortcode takes them and shows them on the main page and anywhere else I specify the shortcode. I use this on quite a lot of sites and never had any problems.

On a current project, the client wants to be able to post Vines on the homepage. I've written another shortcode to embed Vines into a post:

function render_vine_embeds($atts) {

    extract(shortcode_atts(array(
        "id" => '',
        "type" => 'simple',
        "dimension" => 600
    ), $atts));

    $vine_id = $id;
    $vine_type = $type;
    $vine_dimension = $dimension;

    return '<iframe class="vine-embed" src="https://vine.co/v/'. $vine_id .'/embed/'. $vine_type .'" width="'. $vine_dimension .'" height="'. $vine_dimension .'" frameborder="0"></iframe> <script async src="//platform.vine.co/static/scripts/embed.js" charset="utf-8"></script>';
} add_shortcode("vine", "render_vine_embeds");

So the shortcode looks like this: [vine id="{{ id here }}"], the Type and Dimension attributes are optional. These are fine when I'm on a particular post but when it's on the homepage and the Loop is being generated by the shortcode, the Vine shortcode doesn't work and the raw shortcode is shown as the content.

I've tried using do_shortcode([vine]) instead of the_content(), and this worked but the id part of the shortcode didn't so the Vine page displayed a "Not Found" page instead of the Vine itself.

So after all that, my ultimate question is how do I use a shortcode inside a shortcode with a variable?

1 Answer 1

0

You need to apply the do_shortcode to the content itself. Example below:

    function test_func( $atts, $content ) {
         extract( shortcode_atts( array(
              'foo' => 'no foo',
              'baz' => 'default baz'
         ), $atts ) );

        return "<div class='test'>".do_shortcode($content)."</div>';
    }
    add_shortcode( 'test', 'test_func' );

So in other words you need to care about the shortcode that print the posts, no the vine one.

1
  • yeah this works but I had found it before you posted it - thanks anyway though :)
    – Tom Oakley
    Commented Dec 18, 2013 at 13:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.