get_the_post_thumbnail函数的使用

更新时间:2022-06-14 22:18:35 来源:青锋建站 作者:青锋建站
  wordpress函数get_the_post_thumbnail用于获得当前文章的缩略图,以下是青锋建站给大家分享的wordpress函数get_the_post_thumbnail的使用。

get_the_post_thumbnail简介

  当一个主题添加了‘post-thumbnail’支持时,将注册一个特殊的‘post-thumbnail’图像大小,这与通过the Settings > Media screen.管理的“ ‘thumbnail’ ”图像大小不同。
当使用the_post_thumbnail() 或相关函数时,默认情况下会使用‘post-thumbnail’的图像大小,尽管可以根据需要指定不同的大小。

get_the_post_thumbnail参数

$post
 (int|WP_Post) (Optional)Post ID或者WP_Post对象.缺省值是全局变量 $post.缺省值是null。
$size
  (string|int[]) (Optional) 图像大小。接受任何注册的图像大小名称,或以像素为单位的宽度和高度值数组(按此顺序)。默认值:'post-thumbnail'。
$attr
  (string|array) (Optional) 字符串或属性数组,缺省为""。

get_the_post_thumbnail返回值

  发布后的缩略图图像标签

get_the_post_thumbnail函数扩展

  如果所需的add_theme_support( 'post-thumbnails' );在当前主题的函数中。php文件被附加到一个钩子上,则必须在触发init钩子之前调用它。对于某些特性来说,初始挂钩可能已经太晚了。如果它附加到一个钩子上,那么它必须是after_setup_theme.。

get_the_post_thumbnail源代码

File: wp-includes/post-thumbnail-template.php
  function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
    $post = get_post( $post );

    if ( ! $post ) {
        return '';
    }

    $post_thumbnail_id = get_post_thumbnail_id( $post );

    /**
     * Filters the post thumbnail size.
     *
     * @since 2.9.0
     * @since 4.9.0 Added the `$post_id` parameter.
     *
     * @param string|int[] $size    Requested image size. Can be any registered image size name, or
     *                              an array of width and height values in pixels (in that order).
     * @param int          $post_id The post ID.
     */
    $size = apply_filters( 'post_thumbnail_size', $size, $post->ID );

    if ( $post_thumbnail_id ) {

        /**
         * Fires before fetching the post thumbnail HTML.
         *
         * Provides "just in time" filtering of all filters in wp_get_attachment_image().
         *
         * @since 2.9.0
         *
         * @param int          $post_id           The post ID.
         * @param int          $post_thumbnail_id The post thumbnail ID.
         * @param string|int[] $size              Requested image size. Can be any registered image size name, or
         *                                        an array of width and height values in pixels (in that order).
         */
        do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );

        if ( in_the_loop() ) {
            update_post_thumbnail_cache();
        }

        // Get the 'loading' attribute value to use as default, taking precedence over the default from
        // `wp_get_attachment_image()`.
        $loading = wp_get_loading_attr_default( 'the_post_thumbnail' );

        // Add the default to the given attributes unless they already include a 'loading' directive.
        if ( empty( $attr ) ) {
            $attr = array( 'loading' => $loading );
        } elseif ( is_array( $attr ) && ! array_key_exists( 'loading', $attr ) ) {
            $attr['loading'] = $loading;
        } elseif ( is_string( $attr ) && ! preg_match( '/(^|&)loading=/', $attr ) ) {
            $attr .= '&loading=' . $loading;
        }

        $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );

        /**
         * Fires after fetching the post thumbnail HTML.
         *
         * @since 2.9.0
         *
         * @param int          $post_id           The post ID.
         * @param int          $post_thumbnail_id The post thumbnail ID.
         * @param string|int[] $size              Requested image size. Can be any registered image size name, or
         *                                        an array of width and height values in pixels (in that order).
         */
        do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );

    } else {
        $html = '';
    }

    /**
     * Filters the post thumbnail HTML.
     *
     * @since 2.9.0
     *
     * @param string       $html              The post thumbnail HTML.
     * @param int          $post_id           The post ID.
     * @param int          $post_thumbnail_id The post thumbnail ID, or 0 if there isn't one.
     * @param string|int[] $size              Requested image size. Can be any registered image size name, or
     *                                        an array of width and height values in pixels (in that order).
     * @param string|array $attr              Query string or array of attributes.
     */
    return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
} 
  以上是青锋建站给大家分享的wordpress函数get_the_post_thumbnail的使用,青锋建站承接网站建设服务,包括织梦建站,phpcms建站,wordpress建站,CMS系统开发,SEO网站优化,网络营销推广,企业邮箱,400电话。
 

转载请注明来源网址:青锋建站-http://www.sjzphp.com/cmsxitong/wordpressxt/get_the_post_thumbnail.html

地方分站

电话 15632335515 | 邮箱 943703539@qq.com | QQ 943703539 | 微信 qingfengjianzhan

Copyright © 2016-2026 青锋建站 版权所有