1

I'm trying to figure out why WP doesn't seem to recognize functions like get_the_post_thumbnail() inside a shortcode loop.

For example, tried adding it to a fresh theme / plugin-less installation. Here's my functions.php:

<?php
add_theme_support( 'post-thumbnails' ); 

function scRecentPhoto() {
global $post;

$photoargs = array(
'post_type' => 'photos',
'posts_per_page' => 1,
 );
$photoloop = new WP_Query($photoargs);

while ( $photoloop->have_posts() ) : $photoloop->the_post(); 
$output = get_the_post_thumbnail(array(280,130));
$output .= '<h4><a href="' . get_permalink() . '">' . get_the_title() . '</a></h4>';
$output .= '<p>' . get_the_excerpt() . '</p>';
endwhile;

return $output;
}
add_shortcode('recentphoto', 'scRecentPhoto');
?>

And I always get a PHP error such as: Fatal error: Call to undefined function get_the_post_thumbnail() in ...

Any ideas? Thanks.

EDIT = Photo CPT example code:

register_post_type( 'photos',
    array(
        'labels' => array(
            'name' => __( 'Photos' ),
            'singular_name' => __( 'Photo' )
        ),
        'public' => true,
        'supports' => array(
            'title',
            'editor',
            'author',
            'thumbnail',
            'excerpt',
            'comments'
        ),
        'capability_type' => 'post',
        'rewrite' => array('slug'=> $slug)
    )
);

EDIT 2 = Did some more testing, and it turns out that the shortcode works if I place it directly in a post / widget / etc. it just wouldn't work where I need it, which is inside a theme options page. Which I'm calling as such:

$homeoptions = get_option('home_options');
$row1box1 = do_shortcode($homeoptions['row1box1']);

if ($row1box1) :
echo '<div id="row4box1" class="box">' . $row1box1 . '</div>';
endif; 

I'm not entirely sure why it won't work inside the option page though, as other basic shortcodes I've had created work just fine. But hopefully this maybe narrows down the problem a bit?

5
  • If you comment out get_the_post_thumbnail() do the other functions (e.g. get_permalink()) work or do all the template tags throw a fatal error?
    – mrwweb
    Commented May 16, 2012 at 4:19
  • Taking out get_the_post_thumbnail(), everything works fine.
    – James
    Commented May 16, 2012 at 4:40
  • What exactly is the point of using a shortcode if you can just call a function instead?
    – mrwweb
    Commented May 16, 2012 at 15:12
  • So you can use in places like the ost editor, theme option pages, etc.
    – James
    Commented May 16, 2012 at 16:25
  • OH! Nvm, sorry. I see why you're doing that. The plot thickens...
    – mrwweb
    Commented May 16, 2012 at 16:27

3 Answers 3

1

In order to use that function you need to declare this in your theme.

add_theme_support( 'post-thumbnails' );

I tested your code and it works fine, the only way I can duplicate your error is to remove the above.

It might also be because you seem to be using a CPT called "photos", have you made sure it supports the right parameters, specifically,

// rest of your CPT params
'supports' => array('thumbnail'),

http://codex.wordpress.org/Function_Reference/register_post_type

You can test this quickly by trying,

add_post_type_support( 'photos', 'thumbnail' ) 
6
  • I do have "add_theme_support( 'post-thumbnails' );" added, which is listed in my code above, and yes, I do have thumbnails supported inside my CPT file. Thanks for looking into it though. Where do I paste "add_post_type_support( 'photos', 'thumbnail' )" to test things out? Tried pasting that in functions.php but still get the fatal error.
    – James
    Commented May 16, 2012 at 6:03
  • If your CPT supports thumbnails then don't bother. I tested your code on base install of twenty eleven and it works fine. I tested it on a CPT and for the regular post types for 'post_type'. You might have a name conflict with the word "photos", name your CPT something else.
    – Wyck
    Commented May 16, 2012 at 6:46
  • Just tried several other CPTs I have registered as well, none seemed to work (even tried 'post_type' => 'post'). Not sure what other code in the theme could be affecting it.
    – James
    Commented May 16, 2012 at 7:13
  • Although, when I change the CPT type I'm using, the installation works at first. However, when I try to add a new post to the CPT I'm trying to query in the shortcode, I now see the fatal error in the spot where you're suppose to be able to edit the slug, really weird. Then when I try to add a featured image to it, the fatal error appears inside the flash uploader, then things get weird on the admin end, after I try to add the image to the post, the entire admin end dies and gets the fatal error, however the front end doesn't receive it, very strange.
    – James
    Commented May 16, 2012 at 7:21
  • Have you tried to just use echo do_shortcode('[row1box1]');
    – Wyck
    Commented May 16, 2012 at 14:02
0

I see the problem now. The first argument is get_the_post_thumbnail() is $ID. So, to fix your issue, change that line to:

get_the_post_thumbnail( get_the_ID(), array(280,130) );
1
  • Thanks for the attempt, although that didn't change anything, still get the error.
    – James
    Commented May 16, 2012 at 4:59
0

Found a solution, just had to copy and paste the original functions from the WP core and rename them for functions.php.

Not sure why this workaround worked in this instance for theme options, but it did.

Thanks for all the help everyone.

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.