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:


2 Answers 2


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;
    $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

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.

  • how do u call this shortcode for my theme's index.php?
    – Zhianc
    Commented Sep 18, 2011 at 6:47
  • Just added an example to the answer. Commented Sep 18, 2011 at 18:58

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' );

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.