【问题标题】:Customizing WordPress Gallery Post Format自定义 WordPress 图库帖子格式
【发布时间】:2018-01-31 06:36:14
【问题描述】:

我当前的主题支持图库帖子格式。图库格式从图库中获取所有图像并以幻灯片形式显示它们。我希望能够将标题和照片信用行添加到每张图片的底部,因为它会随着信息的变化而变化。

当前的图库格式模板如下所示:

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

我想添加以下代码来显示每张图片的标题和照片信用额度。

    <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 wordpress gallery post-format


    【解决方案1】:

    这个怎么样:

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

    或其他版本,直接echoes 输出而不是将其保存在变量中(即$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; ?>
    

    【讨论】:

    • 谢谢@sally。我如何获得 📷图片来源/要显示在摄影师名字的前面?另外,如何让摄影师的名字右对齐,标题左对齐?
    • 我刚刚意识到的另一件事,当使用画廊发布格式时,它会创建一个与特色图像分开的幻灯片。所以我不希望特色图片出现在单个帖子上,但我仍然需要将其附加到帖子上,以便它在主页上显示为缩略图。如何使用此代码做到这一点?
    • 我已经更新了代码。试试看(第一个或第二个版本)并告诉我。
    • 在回复您的第二条评论时,我认为这涉及修改single.php 模板。您可以在Pastebin.com 上分享模板内容/代码并分享其网址吗?或者,如果您不介意,请告诉我您实际使用的主题名称。 (或父主题的名称,如果您使用的是子主题。)
    • 我正在为 colormag 使用自定义子主题
    猜你喜欢
    • 2016-11-29
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 2014-01-03
    • 2013-10-10
    • 2016-12-29
    相关资源
    最近更新 更多