• Hi there,

    I’m trying to do a few things at once using a shortcode. First, I have a custom field called image that you insert a URL into (and then this image is used in the featured posts area, etc.). I’m adding a shortcode to allow the poster options to post the image in the actual post, too (i.e., on top of the post, floated to the left or right, etc. based on CSS). Here’s my current code, which isn’t working:

    function imagetop( $atts, $content = null ) {
    if ($columns="2") echo '<p><img class=" imgposttop" src="'.get_bloginfo('template_url').'/code/timthumb.php?src=' . do_shortcode($content) . '&w=580&zc=1" alt=""></p>';
    else echo '<p><img class=" imgposttop" src="'.get_bloginfo('template_url').'/code/timthumb.php?src=' . do_shortcode($content) . '&w=890&h=150&zc=1" alt=""></p>';
    }

    add_shortcode('imagetop', 'imagetop');

    What I’m looking to do is two things:

    1) Change the width of the image based on the type of template (1 column or 2 column). On each template page, I have <?php $columns = '1'; ?> setting the variable $columns. As you can see above, if the columns variable is set to 1, the image needs to be 890px wide and if it’s 2 columns, it needs to be 580px wide. I thought what I did above would work, but if it did I wouldn’t be here. 😀

    2) I’d also like to do something that pulls the URL set in the custom field (tag of image) if the poster adds one (and then they’d just type [imagetop][/imagetop] for the shortcode…no URL). If there’s a URL specified in $content (like [imagetop]url[/imagetop], then we use that URL instead. This seems easy, but I haven’t found anything that works for that either.

    Any ideas? Thanks!

The topic ‘Shortcode for post image’ is closed to new replies.