WordPress 通过文章ID获取第一张图片
<?php
/**
* Echo first image (if available).
*
* @param int $post_id Post ID.
*/
function wpdocs_echo_first_image( $post_id ) {
$args = array(
'posts_per_page' => 1,
'order' => 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post_id,
'post_status' => null,
'post_type' => 'attachment',
);
$attachments = get_children( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' );
echo '<img src="' . esc_url( wp_get_attachment_thumb_url( $attachment->ID ) ) . '" class="current" />';
}
}
}
原文参考 https://developer.wordpress.org/reference/functions/get_children/#comment-1306
声明:本文采用 BY-NC-SA 协议进行授权,如无注明均为原创,转载请注明转自 你好!刘
本文地址:WordPress 通过文章ID获取第一张图片
本文地址:WordPress 通过文章ID获取第一张图片
发表评论