^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_the_post_thumbnail_url()用法示例

get_the_post_thumbnail_url( int|WP_Post $post = null,  string|array $size = 'post-thumbnail' )

Return the post thumbnail URL.


描述


参数

$post

(int|WP_Post)
(Optional)
Post ID or WP_Post object. Default is global $post.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

Default value: null

$size

(string|array)
(Optional)
Registered image size to retrieve the 源代码 for or a flat array of height and width dimensions.

Default value: 'post-thumbnail'


返回值

(string|false) Post thumbnail URL or false if no URL is available.


源代码

File: wp-includes/post-thumbnail-template.php

function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
	$post_thumbnail_id = get_post_thumbnail_id( $post );
	if ( ! $post_thumbnail_id ) {
		return false;
	}
	return wp_get_attachment_image_url( $post_thumbnail_id, $size );
}

更新日志

Version 描述
4.4.0 Introduced.

相关函数

Uses

  • wp-includes/media.php:
    wp_get_attachment_image_url()
  • wp-includes/post-thumbnail-template.php:
    get_post_thumbnail_id()

Used By

  • wp-includes/post-thumbnail-template.php:
    the_post_thumbnail_url()

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note

    Contributed by a0903l22

    Don’t ignore the first parameter.
    Proper usage of `get_the_post_thumbnail_url()` inside the loop:

    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
      
            /* grab the url for the full size featured image */
            $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); 
     
            /* link thumbnail to full size image for use with lightbox*/
            echo '<a href="'.esc_url($featured_img_url).'" rel="lightbox">'; 
                the_post_thumbnail('thumbnail');
            echo '</a>';
        endwhile; 
    endif; 
  2. Downvoted the example posted from @thelilmercoder as it is not proper usage of this function.

    Proper usage of `get_the_post_thumbnail_url()` inside the loop:

    
    if ( have_posts() ) {
    	while ( have_posts() ) {
    		the_post();
     
    		/* grab the url for the full size featured image */ 
    		$featured_img_url = get_the_post_thumbnail_url('full'); 
    
    		/* link thumbnail to full size image for use with lightbox*/ 
    		echo '<a href="'.$featured_img_url.'" rel="lightbox">'; 
    			the_post_thumbnail('thumbnail');
    		echo '</a>';
    	endwhile; 
    endif; 
    

    Proper usage of `get_the_post_thumbnail_url()` outside the loop:

    
    /* get a specific post object by ID */ 
    $post = get_post(2);
     
    /* grab the url for the full size featured image */ 
    $featured_img_url = get_the_post_thumbnail_url($post->ID, 'full'); 
    
    /* link thumbnail to full size image for use with lightbox*/ 
    echo '<a href="'.$featured_img_url.'" rel="lightbox">'; 
    	the_post_thumbnail('thumbnail');
    echo '</a>';
    

    To display the featured image with the alt tag use something like this

    
    $thumbnail = get_the_post_thumbnail_url();
    
    if ( $thumbnail ) {
    	$alt_text = get_post_meta( $thumbnail->ID, '_wp_attachment_image_alt', true );
    
        if ( ! empty( $thumbnail ) ) {
    		if ( ! empty( $alt_text ) ) {
    			$alt_text = $alt_text;
           } else {
    			$alt_text = __( 'no alt text set', 'textdomain' ); 
           }
    	   echo '';
       }
    }
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈