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?
In order to use that function you need to declare this in your theme.
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,
http://codex.wordpress.org/Function_Reference/register_post_type
You can test this quickly by trying,
I see the problem now. The first argument is
get_the_post_thumbnail()
is $ID. So, to fix your issue, change that line to: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.