Sidney Sousa
Sidney Sousa

Reputation: 3604

How can I add an image inside a shortcode

I have an image displayed in the following manner:

<img src="<?php bloginfo('template_directory')?>/images/logo/logo.png">

Now, however, I want to create a shortcode that has an attribute and uses that same image inside it. So I tried the following:

add_shortcode('scrollTop', function(){
    echo "<a href='#content' class='content'>";
    echo "<img src='bloginfo('"template_directory"')/images/logo/logo.png'">;
    echo "skipping content";
    echo "</a>";
});

Its a syntax error I assume, but am struggling to find a way around.

How can I add the image inside the shortcode?

Upvotes: 0

Views: 1641

Answers (1)

Tom
Tom

Reputation: 4672

Use the concatenation operator and correct single quotes inside the bloginfo function.

add_shortcode('scrollTop', function(){
    echo "<a href='#content' class='content'>";
    echo "<img src='" . bloginfo('template_directory') . "/images/logo/logo.png'>";
    echo "skipping content";
    echo "</a>";
});

Also make sure to not use echo inside your shortcode to display something. Use return instead. See this question for more information.

add_shortcode('scrollTop', function(){
    return "<a href='#content' class='content'>
        <img src='" . bloginfo('template_directory') . "/images/logo/logo.png'>
        skipping content
        </a>";
});

Upvotes: 1

Related Questions