Barbara D.
Barbara D.

Reputation: 23

WP shortcode for linking image to external URL

I want to add a shortcode named 'image_code' which should link to an external URL for my portfolio image.

In shortcodes.php I added:

add_shortcode('image_code');
function image_code($atts, $content = null) {
    $url = esc_url( $content );
    return "<a href='$url'><image src='$url' /></a>";
}

In the wordpress portfolio site I added:

[portfolio_item columns="4" image="http://www.nairobigarage.com/wp-    
content/uploads/2014/09/NG_startup_image_movas.png" title="Movas Group" text="Company that enable  
mobile subscribers to access emergency airtime on credit easily and instantly." 
image_code="http://www.movasgroup.com/" ]

Everything is fine except the link doesn´t link to the movas website, the link is the www.nairobigarage.com so my home link. What am I doing wrong?

Upvotes: 0

Views: 2054

Answers (2)

Barbara D.
Barbara D.

Reputation: 23

I replaced my shortcodes.php with this one http://pastebin.com/3pWhtRTw and added the link parameter in the shortcode, like [portfolio_item link=”http://google.com”]

Now everything is working.

Upvotes: 1

Anoop Asok
Anoop Asok

Reputation: 1337

Please use the below code in shortcode.php.

<?php
    function image_code( $atts, $content="" ) {
        $url = esc_url( $content );
        return '<a href="'.$url.'"><image src="'.$url.'" /></a>';
    }
    add_shortcode( 'portfolio_item', 'image_code' );
?>

Also use the below short code in the page where you want to display the same.

[portfolio_item]http://www.nairobigarage.com/wp-content/uploads/2014/09/NG_startup_image_movas.png[/portfolio_item]

Upvotes: 0

Related Questions