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
Add a comment  | 

1 Answer 1

Reset to default 1

The 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