Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been using custom fields for my wordpress theme, but i'd like to use shortcodes now for easier posting.

so before, in my index.php i have this:

<?php echo get_post_meta($post->ID, "Thumbnail", true);?>

how do i replace it with custom fields? SUch that i can just insert:

[thumbnail]http://domain.com/image.jpg[/thumbnail]
share|improve this question
add comment

2 Answers

A shortcode like this

[thumbnail height=100 width=100]http://urltoimage.com/image.jpg[/thumbnail]

Would need this type of setup

function content_insert_thumbnail($atts, $content = null){
    global $post;
    extract($atts);
    $thumbnail = get_the_post_thumbnail($post->ID, array($height, $width));
    $content .= $thumbnail; //Places the thumbnail after the content
    return $content; //Never echo a value from a shortcode function.
}
add_shortcode('thumbnail', 'content_insert_thumbnail');

To call this in your theme to insert your thumbnail:

$atts = array(
    'height' => 100,
    'width' => 100
);
content_insert_thumbnail($atts);

This example doesn't make sense to place in your theme as you can just call the_post_thumbnail() function. However, this example is just to show you how to access the shortcode function (whatever it may be) in your template files.

share|improve this answer
    
how do u call this shortcode for my theme's index.php? –  Zhianc Sep 18 '11 at 6:47
    
Just added an example to the answer. –  Brian Fegter Sep 18 '11 at 18:58
add comment

I don't know whether this will work or not, but just try this code

function customfield_shortcode(){
 echo get_post_meta($post->ID, "Thumbnail", true);
//instead echo use return
}
add_shortcode( 'thumbnail', 'customfield_shortcode' );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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