【问题标题】:get image caption from meta box query从元框查询中获取图片标题
【发布时间】:2013-03-29 06:48:48
【问题描述】:

我想将每张图片的图片说明拉到轮播中。我可以成功拉出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>

【问题讨论】:

    标签: php wordpress caption meta-boxes


    【解决方案1】:

    我不确定您如何使用元框(为什么? >

    $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;
                    } 
                 }
    

    【讨论】:

    • 谢谢。这行得通。描述的一个小调整是 $description = $attachment->post_content;
    • 只是跟进,您所说的“您如何使用元框(以及为什么??)是什么意思?主要是和为什么部分。还有什么替代方案?
    • 正确..谢谢。我从我的某个插件中获取了这个脚本,它实际上使用了另一个名为 image 的对象,我忘了更改它。幸好你发现了。
    • 关于元框,我不明白这里的作用是什么。您正在将这些图像用于全局(意味着使用多个帖子)或本地(意味着仅来自单个帖子)的轮播?
    • 适用于单个自定义帖子类型。每个条目都会有自己的一组图像和图像相关信息,每个帖子都会在轮播中显示。
    猜你喜欢
    • 1970-01-01
    • 2016-01-08
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多