wordpress获取文章特色图像ID函数get_post_thumbnail_id()

WordPress教程 4383

wordpress文章缩略图(特色图像)是常用的功能,wordpress函数get_post_thumbnail_id()便是获取文章缩略图ID的一款函数,通过该函数,如果当前文章设置了特色图像,就可以返回该特色图像的ID,如果没有设置则返回null值。

函数结构:
1
<?php get_post_thumbnail_id( $post_id ); ?>
参数说明:

$post_id – 文章ID,默认为空

示例:

1、获取当前日志除了缩略图之外的所有的附件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$args = array(
	'post_type'   => 'attachment',
	'numberposts' => -1,
	'post_status' => null,
	'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 );
	}
}
?>

2、获取当前日志缩略图url地址

1
2
3
4
<?php 
	wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()));
	echo $thumbnail_src[0];
?>
函数位置:

wp-includes/post-thumbnail-template.php

精品推荐: