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

时间:2013-03-22 23:56:24

标签: php wordpress caption meta-boxes

我想将每张图片的图片标题拉入旋转木马。我可以成功拉出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>

1 个答案:

答案 0 :(得分:1)

我不确定你如何使用你的元数据(以及为什么??),也不知道为什么你会在代码中进行直接的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中的atatchments实际上是一个内置的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;
                } 
             }