【问题标题】:Image Caption with the_post_thumbnail in WordPressWordPress中带有the_post_thumbnail的图片说明
【发布时间】:2016-08-19 17:08:03
【问题描述】:

在 WordPress 的主循环中的帖子上显示 the_post_thumbnail() 图像时,有没有办法在可用的地方显示图像标题。

谢谢!感谢所有帮助。

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    这是一个更简单、更简短的代码:

    <?php the_post_thumbnail();
    echo get_post(get_post_thumbnail_id())->post_excerpt; ?>
    

    【讨论】:

      【解决方案2】:

      从 WordPress 4.6 开始,函数 the_post_thumbnail_caption() 已添加到核心 (/wp-includes/post-thumbnail-template.php)。

      使用这里贴的代码会报错:

      Fatal error: Cannot redeclare the_post_thumbnail_caption()
      

      【讨论】:

      • 我的主题有一个功能已经存在于你提到的 Wordpress (4.6) 核心文件中。所以我在主题文件中的函数之前添加了:“if(!function_exists('the_post_thumbnail_caption')) { ... } 并修复了它。谢谢!
      【解决方案3】:

      我想通了:

      /************************************************************\
      * Fetch The Post Thumbnail Caption
      \************************************************************/
      
      function the_post_thumbnail_caption() {
        global $post;
      
        $thumbnail_id    = get_post_thumbnail_id($post->ID);
        $thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
      
        if ($thumbnail_image && isset($thumbnail_image[0])) {
          echo $thumbnail_image[0]->post_excerpt;
        }
      }
      

      【讨论】:

        【解决方案4】:
        if(!function_exists('get_post_thumbnail_caption')) {
            function get_post_thumbnail_caption($post_id = null) {
                $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
                $thumbnail_id = get_post_thumbnail_id($post_id);
                if ($thumbnail = get_post($thumbnail_id))
                    return $thumbnail->post_excerpt;
                return '';
            }
        }
        
        if(!function_exists('the_post_thumbnail_caption')) {
            function the_post_thumbnail_caption($post_id = null) {
                echo get_post_thumbnail_caption($post_id);
            }
        }
        
        if(has_post_thumbnail()) {
            the_post_thumbnail();
            the_post_thumbnail_caption();
        
            $caption = get_post_thumbnail_caption(123);
            if('' == $caption)
                echo '<div class="caption">'.$caption.'</div>';
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-07-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多