0

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

函数.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>

我只希望图像信息显示在图像上,您可以看到我没有当前功能,只需将该信息放入循环中以显示每张幻灯片。

4

2 回答 2

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 } ?>

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

于 2013-04-12T14:10:35.330 回答
0

图像alt文本存储在post_meta表格中,meta_key _wp_attachment_image_altwhile title, caption,description存储在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;
于 2013-04-12T14:15:46.037 回答