WordPress函数get_post_thumbnail_id()获取文章缩略图ID

WordPress函数get_post_thumbnail_id()获取文章缩略图ID

WordPress函数get_post_thumbnail_id()获取文章缩略图ID

  在 WordPress CMS内容管理系统中,get_the_author_meta()是一个内置函数,用于获取帖子的特色图像的 ID。特色图像也称为帖子缩略图

  推荐:The Plus Addon For Elementor插件Elementor扩展插件

get_post_thumbnail_id()函数基本语法

$thumbnail_id = get_post_thumbnail_id($post);

描述

  检索当前帖子作者的请求数据

用法

get_the_author_meta( $field, $user_id );

参数

  $post

  int|WP_Post)可选。帖子 ID 或 WP_Post 对象。默认是全局的$post。默认值:null

  推荐:8个最佳Chrome ChatGPT扩展程序

get_post_thumbnail_id()函数

  get_post_thumbnail_id()函数以整数形式返回特征图像的 ID。如果帖子没有特色图片,则该函数将返回 0。(源文件可参考这里

function get_post_thumbnail_id( $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$thumbnail_id = (int) get_post_meta( $post->ID, '_thumbnail_id', true );

	/**
	 * Filters the post thumbnail ID.
	 *
	 * @since 5.9.0
	 *
	 * @param int|false        $thumbnail_id Post thumbnail ID or false if the post does not exist.
	 * @param int|WP_Post|null $post         Post ID or WP_Post object. Default is global `$post`.
	 */
	return (int) apply_filters( 'post_thumbnail_id', $thumbnail_id, $post );
}

  推荐:NEX-Forms插件下载WordPress表单生成器插件+ Addons

如何使用get_the_author_meta()

  显示帖子缩略图 URL,检索帖子缩略图 ID 并使用它来显示帖子缩略图 URL。

$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_url($thumbnail_id);
echo 'Thumbnail URL: ' . $thumbnail_url;

  从图库中排除帖子缩略图,从附加图像库中排除帖子缩略图。

$args = array(
  'post_type' => 'attachment',
  'numberposts' => -1,
  'post_status' => 'any',
  'post_parent' => $post->ID,
  'exclude' => get_post_thumbnail_id(),
);
$attachments = get_posts($args);

  显示除帖子缩略图之外的所有附加图像,显示帖子的所有附加图像,不包括帖子缩略图。

$exclude_thumbnail_id = get_post_thumbnail_id();
$args = array(
  'post_parent' => $post->ID,
  'post_type' => 'attachment',
  'post_mime_type' => 'image',
  'orderby' => 'menu_order',
  'order' => 'ASC',
  'exclude' => $exclude_thumbnail_id,
);
$attachments = get_posts($args);

  显示帖子缩略图标题,使用缩略图 ID 显示帖子缩略图的标题。

$thumbnail_id = get_post_thumbnail_id();
$thumbnail_caption = get_the_excerpt($thumbnail_id);
echo 'Thumbnail Caption: ' . $thumbnail_caption;

  检查帖子是否有缩略图,检查帖子是否有缩略图并显示相应的消息。

if (has_post_thumbnail()) {
  echo 'This post has a thumbnail.';
} else {
  echo 'This post does not have a thumbnail.';
}

  推荐:如何检查WordPress主题是否存在恶意代码

  推荐:WordPress函数使用手册

5/5 - (1 vote)

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部