WordPress获取文章图片附件函数get_children

网站APP 12 个月前 回复

, ,

			$args = array(
				'order'          => 'DESC',
				'post_mime_type' => 'image',
				'post_parent'    => get_the_ID(),
				'post_status'    => null,
				'post_type'      => 'attachment',
			);
			$attachments = get_children( $args );
			foreach ( $attachments as $attachment ) {
				$output .= '<figure class="wp-block-image wp-image-'.$attachment->ID.'" style="float:left;margin-right:10px;"><img loading="lazy" src="' . esc_url( wp_get_attachment_thumb_url( $attachment->ID ) ) . '" class="wp-image-'.$attachment->ID.'" width="180" height="120" style="height:120px !important" />';
				$output .= '<p style="width:180px; margin-left: 5px; font-size: 0.8rem;"><a href="'.esc_url( get_permalink()).'" title="'.esc_html(get_the_title()).'" >'.wp_trim_words(get_the_title(), 8).'</a></p></figure><!-- .wp-block-image -->';
			}

参考官方文档:https://developer.wordpress.org/reference/functions/get_children/

支付宝打赏微信打赏

如果此文对你有帮助,欢迎打赏作者。

发表评论

欢迎回来 (打开)

(必填)