向标题添加图像标题和/或说明 [英] Add image title and/or description to the caption

查看:0
本文介绍了向标题添加图像标题和/或说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以处理the_content中的图像标题的输出,以便在每个图像下不仅输出标题,还输出图像标题和/或描述(在媒体库中定义)?

可能通过函数.php?

推荐答案

是,可以操作the_content

看起来您想要做的最简单的方法是在您的unctions.php文件中添加筛选器。

我只是从法典中摘录了这篇文章,并做了一些修改,这样你就可以理解我所说的大意了。

add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
 
function my_img_caption_shortcode( $output, $attr, $content ) {
    $attr = shortcode_atts( array(
        'id'      => '',
        'align'   => 'alignnone',
        'width'   => '',
        'caption' => ''
    ), $attr );
 
    if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
        return '';
    }
 
    if ( $attr['id'] ) {
        $attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';
    }
 
    return '<div ' . $attr['id']
    . 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
    . 'style="max-width: ' . ( 10 + (int) $attr['width'] ) . 'px;">'
    . do_shortcode( $content )
    . '<p class="wp-caption-text">' . $attr['caption'] . '</p>'
    . '</div>';
 
}
然后您可以做的是,如果您看到$attr['caption'],您可以修改您希望它是什么样子。因此,您可以执行以下操作:get_the_title($attr['id'])添加标题或get_post_meta($attr['id'], '_wp_attachment_image_alt', TRUE);获取图像ALT。

若要获取特定附件的所有详细信息,可以使用this这样的函数,然后获取您想要的任何内容。

下面是将标题添加到标题的示例。格式为";标题-标题&q;

. '<p class="wp-caption-text">' . get_the_title($attr['id']) . " - " . $attr['caption'] . '</p>'

代码引用链接:https://developer.wordpress.org/reference/hooks/img_caption_shortcode/

这篇关于向标题添加图像标题和/或说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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