admin管理员组文章数量:1122832
I assigned my posts archive to a site on settings -> reading. I added a post thumbnail to this site, and i want to show the image with a shortcode i have written in my plugin. The problem is, that the shortcode returns the wrong ID for archive pages (it returns zero). So the first image of the last post is returned as image. On a normal site the shortcode is working fine, because the ID is returned correctly.
function lwb_image_shortcode( $atts ) {
// Attribute für den Shortcode festlegen
$atts = shortcode_atts(
array(
'type' => 'featured', // Welches Bild, Standard = Haupt Beitragsbild
'return' => 'full', // Rückgabewert - Full = HTML Image Element | URL = Bild URL
'size' => 'medium', // Wordpress Bildgrössen Name
),
$atts, 'lwb_image'
);
if( $atts['return'] == 'full' ) {
$image = wp_get_attachment_image( get_post_thumbnail_id(), $atts['size'], '', '' );
} elseif( $atts['return'] == 'url' ) {
$image = wp_get_attachment_image_url( get_post_thumbnail_id(), $atts['size'], '', '' );
}
return $image;
}
add_shortcode('lwb_image', 'lwb_image_shortcode');
I assigned my posts archive to a site on settings -> reading. I added a post thumbnail to this site, and i want to show the image with a shortcode i have written in my plugin. The problem is, that the shortcode returns the wrong ID for archive pages (it returns zero). So the first image of the last post is returned as image. On a normal site the shortcode is working fine, because the ID is returned correctly.
function lwb_image_shortcode( $atts ) {
// Attribute für den Shortcode festlegen
$atts = shortcode_atts(
array(
'type' => 'featured', // Welches Bild, Standard = Haupt Beitragsbild
'return' => 'full', // Rückgabewert - Full = HTML Image Element | URL = Bild URL
'size' => 'medium', // Wordpress Bildgrössen Name
),
$atts, 'lwb_image'
);
if( $atts['return'] == 'full' ) {
$image = wp_get_attachment_image( get_post_thumbnail_id(), $atts['size'], '', '' );
} elseif( $atts['return'] == 'url' ) {
$image = wp_get_attachment_image_url( get_post_thumbnail_id(), $atts['size'], '', '' );
}
return $image;
}
add_shortcode('lwb_image', 'lwb_image_shortcode');
Share
Improve this question
asked Mar 13, 2024 at 12:58
LovinQuaQuaLovinQuaQua
733 silver badges18 bronze badges
1 Answer
Reset to default 1The default WP_Query
object on the blog homepage is the feed of blog posts rather than the page itself, so you'll need to grab the post ID manually and pass it to get_post_thumbnail_id()
:
function lwb_image_shortcode( $atts ) {
// Attribute für den Shortcode festlegen
$atts = shortcode_atts(
array(
'type' => 'featured', // Welches Bild, Standard = Haupt Beitragsbild
'return' => 'full', // Rückgabewert - Full = HTML Image Element | URL = Bild URL
'size' => 'medium', // Wordpress Bildgrössen Name
),
$atts, 'lwb_image'
);
if( is_home() ) {
$id = get_post_thumbnail_id( (int) get_option( 'page_for_posts' ) );
}
else {
$id = get_post_thumbnail_id();
}
if( $atts['return'] == 'full' ) {
$image = wp_get_attachment_image( $id, $atts['size'], '', '' );
} elseif( $atts['return'] == 'url' ) {
$image = wp_get_attachment_image_url( $id, $atts['size'], '', '' );
}
return $image;
}
add_shortcode('lwb_image', 'lwb_image_shortcode');
本文标签: shortcodeGetting the wrong featured image on archive page
版权声明:本文标题:shortcode - Getting the wrong featured image on archive page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736312261a1935037.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论