如何获取WP Gallery图像信息以进行循环

时间:2013-04-12 13:52:39

标签: php wordpress slider image-gallery

我正在制作一个网站的滑块,图片来自WP帖子的图库,效果很好。但是我想要那里的图像信息,例如图像的标题和描述。这是我当前的滑块代码和循环,它完美无缺。

的functions.php:

<?php function aldenta_get_images($size = 'thumbnail') {
    global $post;

    $photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

    $results = array();

    if ($photos) {
        foreach ($photos as $photo) {
            // get the correct image html for the selected size
            $results[] = wp_get_attachment_image($photo->ID, $size);
        }
    }

    return $results;
} ?>

页:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <div class="banner">

                <ul class="rslides">
                            <?php $galleryimages = aldenta_get_images('medium');
                            foreach ($galleryimages as $galleryimage ) { ?>
                        <li>
                            <?php echo $galleryimage; ?>

                                <div class="slider-desc">
                                    <div class="slider-desc-text">
                                    <div class="slider-desc-title"><h1></h1></div>
                                    <div class="slider-desc-content"><?php  ?></div>
                                    <div class="slider-link"><a href="#"><img src="<?php bloginfo('template_directory'); ?>/images/slider-arrow.png"></a></div>
                                    </div>
                                </div>

                            <?php } ?>

                        </li>

                        <?php endwhile; else: ?>
                        <?php endif; ?>

                </ul>

我只想在图像上显示图像信息,你可以看到我没有当前的功能,只需要将信息输入循环以显示每张幻灯片。

2 个答案:

答案 0 :(得分:0)

在你的function.php文件中,你可以:

    foreach ($photos as $photo) {
        // get the correct image html for the selected size
        $results[]['content'] = wp_get_attachment_image($photo->ID, $size);
        $results[]['title'] = get_the_title($photo->ID);
    }

然后

                        <?php $galleryimages = aldenta_get_images('medium');
                        foreach ($galleryimages as $galleryimage ) { ?>
                    <li>
                        <?php echo $galleryimage['content']; ?>

                            <div class="slider-desc">
                                <div class="slider-desc-text">
                                <div class="slider-desc-title"><h1><?php echo $galleryimage['title']; ?></h1></div>
                                <div class="slider-desc-content"><?php  ?></div>
                                <div class="slider-link"><a href="#"><img src="<?php bloginfo('template_directory'); ?>/images/slider-arrow.png"></a></div>
                                </div>
                            </div>

                        <?php } ?>

此处的示例仅用于获取标题。我相信你可以对内容或任何你想要的东西(例如元数据)做同样的事情

答案 1 :(得分:0)

图片alt文字存储在post_meta表格中meta_key _wp_attachment_image_alt titlecaptiondescription存储在wp_posts以下是演示如何获取这些

$alt = get_post_meta($photo->ID, '_wp_attachment_image_alt', true);
if(count($alt)) echo $alt;

$image_title = $photo->post_title;
$caption = $photo->post_excerpt;
$description = $photo->post_content;