从元框查询中获取图像标题


get image caption from meta box query

我想将每个图像的图像标题拉到旋转木马中。我可以成功地拉出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插件来拉图像。

Obmerk说对了。不需要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>

我不确定你如何使用你的metabox(为什么?? ?),也不知道为什么你会在代码中做一个直接的SQL查询,但通常情况下,这就是你如何拉图像(附件)数据:

$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $image->post_content;

由于wp中的附件实际上是内置的CPT(实际上是一个帖子):

附件标题实际上是文章的摘录附件标题是帖子标题附件描述为帖子内容。

所以一个完整的代码应该是这样的:

$args = array( 'post_type' => 'attachment', 
                    'orderby' => 'menu_order', 
                    'order' => 'ASC', 
                    'post_mime_type' => 'image' ,
                    'post_status' => null, 
                    'numberposts' => null, 
                    'post_parent' => $post->ID );
        $attachments = get_posts($args);
        if ($attachments) {
            foreach ( $attachments as $attachment ) {
                $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
                $image_title = $attachment->post_title;
                $caption = $attachment->post_excerpt;
                $description = $attachment->post_content;
                } 
             }