从Wordpress Gallery图像获取图像标题

时间:2017-05-12 02:19:11

标签: php wordpress

<?php $loop = new WP_Query( array( 'post_type' => 'gallery', 
        'posts_per_page' => 100 ) 
            ); 
        while ( $loop->have_posts() ) : $loop->the_post(); ?>




    <?php if ( get_post_gallery() ) :


            /* Loop through all the image and output them one by one */
            foreach( $gallery['src'] as $src ) : ?>

                <img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
                <?php
            endforeach;
        endif;

 endwhile; wp_reset_query(); ?>

上面的代码从名为&#34; gallery的自定义帖子中提取WordPress库。&#34;然后它存储并显示图像。有没有办法将图库的标题存储到变量中?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码从WordPress库中获取图片标题。

<?php $loop = new WP_Query( array( 'post_type' => 'gallery', 
        'posts_per_page' => 100 ) 
            ); 
        while ( $loop->have_posts() ) : $loop->the_post();

        if ( $gallery = get_post_gallery( get_the_ID(), false ) ) :

            $img_ids = explode( ',', $gallery['ids'] );
            /* Loop through all the image and output them one by one */
            foreach( $gallery['src'] as $key => $src ) : ?>

                <img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
                <?php
                    $image_post = get_post( $img_ids[ $key ] ); ?>
                    <p class="wp-caption-text"><?php echo $image_post->post_excerpt; ?></p>

            <?php endforeach;
        endif;

 endwhile; wp_reset_postdata(); ?>