【问题标题】:Get Image Caption from Wordpress Gallery Images从 Wordpress 图库图片中获取图片标题
【发布时间】:2017-05-12 02:19:11
【问题描述】:
<?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(); ?>

上面的代码从名为“gallery”的自定义帖子中提取了一个 WordPress 画廊。然后它存储并显示图像。有没有办法将画廊的标题也存储到变量中?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以使用以下代码从 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(); ?>
    

    【讨论】:

      猜你喜欢
      • 2014-06-03
      • 1970-01-01
      • 2011-11-08
      • 2019-12-11
      • 1970-01-01
      • 2014-11-16
      • 2014-08-17
      • 2016-01-08
      相关资源
      最近更新 更多