【问题标题】:wordpress display image caption if possible, but only on post page如果可能,wordpress 显示图片标题,但仅在帖子页面上
【发布时间】:2013-04-17 15:31:51
【问题描述】:

我正在尝试制作一个可以放入我的functions.php 的函数,该函数控制放置在每个帖子页面上的图像。我希望任何带有标题的图片都能在带有段落标签的图片下方显示该标题。

【问题讨论】:

    标签: image wordpress caption


    【解决方案1】:

    试试这个:

    function img_caption_only_in_posts( $current_html, $attr, $content ) {
    global $post;
        extract(shortcode_atts(array(
            'id'    => '',
            'align' => 'alignnone',
            'width' => '',
            'caption' => ''
        ), $attr));
        if ( 1 > (int) $width || empty($caption) )
            return $content;
    
        if ($post->post_type=='page')
         $caption="";
    
        if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
    
        return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
    . do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
    }
    add_filter( 'img_caption_shortcode', 'img_caption_only_in_posts',10,3 );
    

    我从this answer借用了代码,并添加了用于检查帖子是否为页面的代码。

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 1970-01-01
      • 2011-08-09
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 2017-07-20
      相关资源
      最近更新 更多