wordpress 制作产品多图循环的方法及相册调用方式
wordpress 调用文章相册中所有的图片及图片名称和说明文字
在WordPress中,可以使用内置的get_children
函数来获取指定文章的所有附件(包括图片)。以下是一个简单的代码示例,展示如何调用指定文章(通过$post_id
变量)中所有图片的信息,包括图片链接和说明文字:
<?php
$post_id = get_the_ID(); // 获取当前文章的ID
$args = array(
'post_parent' => $post_id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'order' => 'ASC',
'orderby' => 'menu_order ID'
);
$attachments = get_children($args); // 获取所有附件
if ($attachments) {
foreach ($attachments as $attachment) {
// 获取图片链接
$image_url = wp_get_attachment_image_src($attachment->ID, 'full');
$image_url = $image_url[0];
// 获取图片名称
$image_title = get_the_title($attachment->ID);
// 获取图片描述
$image_description = $attachment->post_content;
// 获取图片说明
$image_caption = trim(wp_get_attachment_caption($attachment->ID));
echo'<li style="display: list-item;">
<div class="two_text">
<h3>
<em></em> ' . esc_attr($image_caption) .'
</h3>
<h4>
作品描述:
</h4>
<p>
'. $image_description.'
</p>
</div>
<div class="two_img">
<img src="'. $image_url .'">
</div>
</li>';
}
}
?>
留下评论