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?
get_the_post_thumbnail()
do the other functions (e.g.get_permalink()
) work or do all the template tags throw a fatal error?