admin管理员组文章数量:1122846
i'm working on a WordPress plugin and i'm trying to display/get the post thumbnail below the post on my "Related posts" part. I tried many things which are supposed to work but not for me ... here is the code (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');
}
first i thought that there were no thumbnail for my post but my first echo returned "there is a thumbnail" and all the rest doesn't return anything ... so i don't understand... what is missing ?
Thanks
Edit:
here is the current full code in my plugin.php file:
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' );
my function works if there is a thumbnail but display nothing if there is no thumbnail...
i'm working on a WordPress plugin and i'm trying to display/get the post thumbnail below the post on my "Related posts" part. I tried many things which are supposed to work but not for me ... here is the code (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');
}
first i thought that there were no thumbnail for my post but my first echo returned "there is a thumbnail" and all the rest doesn't return anything ... so i don't understand... what is missing ?
Thanks
Edit:
here is the current full code in my plugin.php file:
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' );
my function works if there is a thumbnail but display nothing if there is no thumbnail...
Share Improve this question edited Jun 12, 2017 at 16:46 bravokeyl 3,3776 gold badges27 silver badges33 bronze badges asked Jun 12, 2017 at 7:41 Cédric CossonCédric Cosson 12 bronze badges1 Answer
Reset to default 0Please notice, has_post_thumbnail()
does not only check to see if the post has a featured image, but also checks if there is an image in the post itself. There is a quote from WordPress Code Reference:
It’s worth noting that
has_post_thumbnail()
does not just check for the Featured Image as the Codex User Contributed Note suggests. If a post contains no defined featured image but it does contain an image in the content this function will still return TRUE.
I suggest you use if (get_the_post_thumbnail() != '')
instead of if (has_post_thumbnail())
.
本文标签: pluginsCan39t get or display post thumbnail in wordpress
版权声明:本文标题:plugins - Can't get or display post thumbnail in wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281231a1926250.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论