我正在开发一个WordPress插件,并试图在“相关帖子”部分的帖子下方显示/获取帖子缩略图。我尝试了很多本该有用但对我来说不合适的东西。。。这是代码(on my plugin.php file):
add_theme_support( \'post-thumbnails\', array( \'post\', \'page\' ) );
if ( has_post_thumbnail() ) {
echo (\'there is a thumbnail\');
echo get_the_post_thumbnail(get_the_ID(), "thumbnail"); // nothing
echo get_the_post_thumbnail(); // nothing
echo var_dump(get_the_post_thumbnail(get_the_ID(), "thumbnail")); //string \'\' (length=0)
echo var_dump(get_the_post_thumbnail()); //string \'\' (length=0)
the_post_thumbnail(\'thumbnail\'); // nothing
the_post_thumbnail(); // nothing
}
else {
echo (\'there is no thumbnail\');
}
首先,我认为我的帖子没有缩略图,但是
my first echo returned "there is a thumbnail" 其余的都不会返回任何东西。。。所以我不明白。。。缺少什么?
谢谢
Edit:
以下是我的
plugin.php 文件:
function Similar_Article ($content) {
if (is_single() || is_page()) {
the_post();
$similar_to_add = \'\';
$id = get_the_ID();
$content_and_similar = $content . \'<br><br>\'; //spaces between post and related posts
/* many methods called to find my related posts */
if (has_post_thumbnail()){
$content_and_similar .= get_the_post_thumbnail(); // it works
}
else {
$imageData = wp_get_attachment_image_src(get_post_thumbnail_id ( $id ), \'thumbnail\');
$content_and_similar .= \'<img src=" \'.$imageData[0].\' ">\';
}
$content_and_similar .= $similar_to_add;
return $content_and_similar;
}
else {
return $content;
}
}
add_filter( \'the_content\', \'Similar_Article\' );
如果有缩略图,我的函数可以工作,但如果没有缩略图,则不显示任何内容。。。