我想将每个图像的图像标题拉到轮播中。我可以成功拉出 alt 标签,但我无法获得标题。
这是我到目前为止所拥有的,但它正在打破。
<?php $args = array(
'post_type' => 'homepageslider',
'posts_per_page' => 1
);
$query = new WP_Query($args);
while ($query->have_posts()):
$query->the_post();
$meta = get_post_meta( get_the_ID( ), 'homepage_media_gallery', false );
if ( !is_array( $meta ) )
$meta = ( array ) $meta;
if ( !empty( $meta ) ) {
$meta = implode( ',', $meta );
$images = $wpdb->get_col( "
SELECT ID FROM $wpdb->posts
WHERE post_type = 'attachment'
AND ID IN ( $meta )
ORDER BY menu_order ASC
" );
foreach ( $images as $att ) {
// Get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
$attachment_meta = wp_get_attachment($att);
$link = $attachment_meta['caption'];
$src = wp_get_attachment_image_src( $att, 'full' );
$alt = get_post_meta($att, '_wp_attachment_image_alt', true);
$src = $src[0];
// show caption
echo $link;
// Show image
echo "<div><img src='{$src}' class='project-images' />"; ?>
<?php if ($alt) : ?>
<div class='homepage-caption'>
<?php echo $alt; ?>
</div>
<?php endif ?>
<?php echo "</div>";
}
}
endwhile
?>
你如何获得查询中每张图片的标题?我正在使用 WP Meta Boxes 插件来提取图像。
更新
奥默克说得对。不需要 SQL 查询。这就是我现在所拥有的,它为每个图像提取 alt、标题和描述。
<?php
$args = array( 'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image' ,
'post_status' => null,
'numberposts' => null,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
);
$attachments = get_posts($args); ?>
<?php if ($attachments) : ?>
<?php foreach ( $attachments as $attachment ): ?>
<?php
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $attachment->post_content;
$src = wp_get_attachment_image_src( $attachment->ID, 'full' );
list($width, $height, $type, $attr) = getimagesize($src[0]); ?>
<div class="projectBlock">
<div class="projectCopy">
<h2><?php echo $alt ?></h2>
<h3><?php echo $caption ?></h3>
<p class='projectDescription'><?php echo $description ?></p>
</div>
<div class="projectImage">
<img src="<?php echo $src[0]; ?>" class="project-images
</div>
</div>