我在一个网站上工作,该网站具有输出图片标题的功能,是信用/日期,但如果只定义了信用/日期,而标题不存在,则不起作用。如果是这种情况,函数将忽略图像,并将其包装在一个p标记中,我认为默认情况下,它是由wordpress生成的,我甚至无法理解代码的来源。有没有办法让这个函数在没有标题的情况下输出信用/日期?
function update_caption_shortcode( $empty, $attr, $content ){
$atts = shortcode_atts( array(
\'id\' => \'\',
\'align\' => \'alignnone\',
\'width\' => \'\',
\'caption\' => \'\',
\'class\' => \'\',
), $attr, \'caption\' );
$atts[\'width\'] = (int) $atts[\'width\'];
if ( $atts[\'width\'] < 1 || empty( $atts[\'caption\'] ) )
return $content;
if ( ! empty( $atts[\'id\'] ) )
$atts[\'id\'] = \'id="\' . esc_attr( sanitize_html_class( $atts[\'id\'] ) ) . \'" \';
$class = trim( \'wp-caption \' . $atts[\'align\'] . \' \' . $atts[\'class\'] );
$html5 = current_theme_supports( \'html5\', \'caption\' );
$width = $html5 ? $atts[\'width\'] : ( 10 + $atts[\'width\'] );
$caption_width = apply_filters( \'img_caption_shortcode_width\', $width, $atts, $content );
//Get att id
$att_id = str_replace(\'"\', "", $atts[\'id\']);
$att_id = explode(\'_\', $att_id);
$att_id = intval( $att_id[1] );
$attachment = get_post( $att_id );
if (isset($attachment)) {
$credits = get_post_meta( $att_id, \'photographer_name\', true );
$date = get_post_meta( $att_id, \'photo_date\', true );
}
$style = \'\';
if ( $caption_width )
$style = \'style="width: \' . (int) $caption_width . \'px" \';
$html = \'\';
if ( $html5 ) {
$html = \'<figure \' . $atts[\'id\'] . $style . \'class="\' . esc_attr( $class ) . \'">\'
. do_shortcode( $content ) . \'<figcaption class="image__slide-meta">\';
$html .= ( isset( $atts[\'caption\'] ) && \'\' != $atts[\'caption\'] ) ? \'<span class="caption__text">\' . $atts[\'caption\'] . \'</span>\' : \'\';
$html .= ( isset( $credits ) && \'\' != $credits) ? \'<span class="caption__source">\'.$credits.\'</span> \' : \'\';
$html .= ( isset( $date) && \'\' != $date ) ? \'<span class="caption__date">\'.$date.\'</span>\' : \'\';
$html .= \'</figcaption></figure>\';
} else {
$html = \'<div \' . $atts[\'id\'] . $style . \'class="\' . esc_attr( $class ) . \'">\'
. do_shortcode( $content ) . \'<p class="wp-caption-text">\' . $atts[\'caption\'] . \'</p></div>\';
}
return $html;
}
add_filter( \'img_caption_shortcode\', \'update_caption_shortcode\', 10, 3 );