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

江河/ 2023年05月12日/ WordPress/ 浏览 1676

函数原型:


get_post_thumbnail_id( int|WP_Post $post = null ): int|false


参数说明:


$post,文章 ID 或 文章对象。


需要使用下面的代码,开启文章缩略图功能:


add_theme_support( 'post-thumbnails' ); 


函数源码:


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 );

	return (int) apply_filters( 'post_thumbnail_id', $thumbnail_id, $post );
}


包含钩子:


apply_filters( 'post_thumbnail_id', int|false $thumbnail_id, int|WP_Post|null $post )


使用举例:


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

$attachments = get_posts( $args );

if ( $attachments ) {
	foreach ( $attachments as $attachment ) {
		echo apply_filters( 'the_title', $attachment->post_title );
		the_attachment_link( $attachment->ID, false );
	}
}


获取文章内的图片,但是排除缩略图。


发表评论

暂无评论,抢个沙发...

客服 工单