How to get WordPress post featured image URL

前端 未结 20 1903
闹比i
闹比i 2020-12-02 04:59

I am using this function to get the featured images:


    


        
相关标签:
20条回答
  • 2020-12-02 05:28

    Try this one

    <?php 
        echo get_the_post_thumbnail($post_id, 'thumbnail', array('class' => 'alignleft')); 
    ?>
    
    0 讨论(0)
  • 2020-12-02 05:29

    You can also get it from post_meta like this:

    echo get_post_meta($post->ID, 'featured_image', true);
    
    0 讨论(0)
  • 2020-12-02 05:30

    If you want JUST the source, and not an array with other information:

    <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
    <img src="<?php echo $url ?>" />
    

     

    0 讨论(0)
  • 2020-12-02 05:31
    <?php
        if (has_post_thumbnail( $post->ID ) ):
            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
    ?>
            <img src="<?php echo $image[0]; ?>">  
    <?php endif; ?>
    
    0 讨论(0)
  • 2020-12-02 05:32

    If the post is an image and we already know what the image is, it's possible to get the thumbnail URL without too much hassle:

    echo pathinfo($image->guid, PATHINFO_DIRNAME);
    
    0 讨论(0)
  • 2020-12-02 05:34

    You can also get the URL for image attachments as follows. It works fine.

    if (has_post_thumbnail()) {
        $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium'); 
    }
    
    0 讨论(0)
提交回复
热议问题