自定义WordPress画廊帖子格式 [英] Customizing WordPress Gallery Post Format

查看:167
本文介绍了自定义WordPress画廊帖子格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的主题支持画廊帖子格式. Gallery格式从图库中获取所有图像,并以幻灯片形式显示它们.我希望能够在每幅图像的底部添加标题和照片来源行,以使信息随其改变.

My current theme supports Gallery Post Formats. The Gallery format takes all images from the gallery and displays them in a slideshow. I want to be able to add the caption and photo credit line to the bottom of each image as it changes for the information to change along with it.

当前的画廊格式模板如下:

The current gallery format template looks like this:

 <?php if ( has_post_format( 'gallery' ) ) : ?>

   <div class="gallery-post-format">
      <?php
      $galleries = get_post_gallery_images( $post );

      $output = '<ul class="gallery-images">';
      foreach ($galleries as $gallery) {
         $output .= '<li>' . '<img src="'. $gallery . '">' . '</li>';
      }
      $output .= '</ul>';

      echo $output;
      ?>
   </div>

<?php endif; ?>

我想添加以下代码以在每张图像上显示标题和照片信用额度.

I am wanting to add the following code to display the captions and photo credit line to each image.

    <div>
    <?php 
        $photographer_name = get_post_meta(get_post_thumbnail_id(), 'be_photographer_name', true);
        $photographer_url = get_post_meta(get_post_thumbnail_id(), 'be_photographer_url', true);

        if ( $photographer_name ) : ?>
            <div id="tgg_credit_line" class="tgg-photo-credit" align="right">
                &#x1F4F7; Image Credit /
                <?php if ( $photographer_url ) : ?> 
                <a href="<?php echo $photographer_url ?>">
                <?php endif; ?>
            <?php echo $photographer_name ?>
                <?php if ( $photographer_url ) : ?> 
                </a>
                <?php endif; ?>
            </div>
        <?php endif; ?>
</div>
<div id="tgg_image_caption" class="tgg-image-caption" align="left">
    <?php echo get_post(get_post_thumbnail_id())->post_excerpt; ?>
</div>

</div>

我该怎么做?

推荐答案

这是怎么回事:

<?php if ( has_post_format( 'gallery' ) ) : ?>

    <div class="gallery-post-format">
        <?php
            $gallery = get_post_gallery( $post, false );
            $att_ids = wp_parse_id_list( $gallery['ids'] );

            $output = '<ul class="gallery-images">';
            for ( $i = 0; $i < count( $gallery['src'] ); $i++ ) {
                $att_id = $att_ids[ $i ];
                $photographer_name = get_post_meta( $att_id, 'be_photographer_name', true );
                $photographer_url = get_post_meta( $att_id, 'be_photographer_url', true );

                $image = sprintf( '<img src="%s" />',
                    esc_url( $gallery['src'][ $i ] ) );

                $caption = '<div>';
                if ( $photographer_name ) {
                    $caption .= '<div class="tgg-photo-credit" align="right">';
                    $caption .= '&#x1F4F7; Image Credit /';
                    if ( $photographer_url ) {
                        $caption .= sprintf( '<a href="%s">%s</a>',
                            esc_url( $photographer_url ), esc_html( $photographer_name ) );
                    } else {
                        $caption .= esc_html( $photographer_name );
                    }
                    $caption .= '</div><!-- .tgg-photo-credit -->';
                }
                $caption .= '</div>';

                $excerpt = get_post_field( 'post_excerpt', $att_id );
                if ( $excerpt ) {
                    $caption .= /* wrapped */
                    '<div class="tgg-image-caption" align="left">' .
                        $excerpt .
                    '</div><!-- .tgg-image-caption -->';
                }

                $output .= '<li>' . $image . $caption . '</li>';
            }
            $output .= '</ul><!-- .gallery-images -->';

            echo $output;
        ?>
    </div><!-- gallery-post-format -->

<?php endif; ?>

或另一个版本,该版本直接echo输出而不是将其保存到变量(即$output)中:

Or another version, which directly echoes the output instead of saving it in a variable (i.e. $output):

<?php if ( has_post_format( 'gallery' ) ) : ?>

    <div class="gallery-post-format">
        <?php
            $gallery = get_post_gallery( $post, false );
            $att_ids = wp_parse_id_list( $gallery['ids'] );
        ?>
        <ul class="gallery-images">
            <?php for ( $i = 0; $i < count( $gallery['src'] ); $i++ ) : ?>
                <li>
                    <?php
                        $att_id = $att_ids[ $i ];
                        $photographer_name = get_post_meta( $att_id, 'be_photographer_name', true );
                        $photographer_url = get_post_meta( $att_id, 'be_photographer_url', true );
                        $excerpt = get_post_field( 'post_excerpt', $att_id );
                    ?>

                    <img src="<?php echo esc_url( $gallery['src'][ $i ] ); ?>" />

                    <div>
                        <?php if ( $photographer_name ) : ?>
                            <div class="tgg-photo-credit" align="right">
                                &#x1F4F7; Image Credit /
                                <?php if ( $photographer_url ) : ?>
                                    <a href="<?php echo esc_url( $photographer_url ); ?>"><?php echo esc_html( $photographer_name ); ?></a>
                                <?php else : ?>
                                    <?php echo esc_html( $photographer_name ); ?>
                                <?php endif; // end $photographer_url ?>
                            </div><!-- .tgg-photo-credit -->
                        <?php endif; // end $photographer_name ?>
                    </div>

                    <?php if ( $excerpt ) : ?>
                        <div class="tgg-image-caption" align="left">
                            <?php echo $excerpt; ?>
                        </div><!-- .tgg-image-caption -->
                    <?php endif; // end $excerpt ?>
                </li>
            <?php endfor; // end gallery loop ?>
        </ul><!-- .gallery-images -->
    </div><!-- .gallery-post-format -->

<?php endif; ?>

这篇关于自定义WordPress画廊帖子格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆