我正在使用这段优秀的代码通过插入管道字符的方式在单个图像标题中插入换行符。。。
现在,我希望您的帮助,尝试将这纳入到画廊的图像字幕了。
这是适用于单幅图像标题的原始代码。下面是我对库代码的尝试。
/*
*********************************************
Function to allow multi-line photo captions.
This function will split captions onto multiple lines if it detects
a "|" (pipe) symbol.
**********************************************
*/
/* Override existing caption shortcode handlers with our own */
add_shortcode(\'wp_caption\', \'multiline_caption\');
add_shortcode(\'caption\', \'multiline_caption\');
/* Our new function */
function multiline_caption($attr, $content = null) {
extract(shortcode_atts(array(
\'id\' => \'\',
\'align\' => \'alignnone\',
\'width\' => \'\',
\'caption\' => \'\'
), $attr));
if ( 1 > (int) $width || empty($caption) )
return $content;
if ( $id ) $id = \'id="\' . esc_attr($id) . \'" \';
$new_caption = str_replace("|", "<br />", $caption);
return \'<div \' . $id . \'class="wp-caption \' . esc_attr($align) . \'" style="width: \' . (10 + (int) $width) . \'px">\'
. do_shortcode( $content ) . \'<p class="wp-caption-text">\' . $new_caption . \'</p></div>\'; }
Mark Wood的原始代码:
http://digitalnotions.net/multi-line-captions-in-wordpressHere is my attempt with the gallery shortcode in functions.php file
这基本上是从wp includes/media复制的代码。php gallery函数。
我已经修改了代码末尾的输出函数(原始代码已注释),以尝试将管道插入其中,但它对我的图库标题无效!
非常感谢你的帮助。
remove_shortcode(\'gallery\', \'gallery_shortcode\');
add_shortcode(\'gallery\', \'multiline_caption_gallery\');
/* Our new function */
function multiline_caption_gallery($attr) {
global $post;
static $instance = 0;
$instance++;
extract(shortcode_atts(array(
\'order\' => \'ASC\',
\'orderby\' => \'menu_order ID\',
\'id\' => $post->ID,
\'itemtag\' => \'dl\',
\'icontag\' => \'dt\',
\'captiontag\' => \'dd\',
\'columns\' => 3,
\'size\' => \'thumbnail\',
\'include\' => \'\',
\'exclude\' => \'\'
), $attr));
$id = intval($id);
if ( \'RAND\' == $order )
$orderby = \'none\';
if ( !empty($include) ) {
$include = preg_replace( \'/[^0-9,]+/\', \'\', $include );
$_attachments = get_posts( array(\'include\' => $include, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$exclude = preg_replace( \'/[^0-9,]+/\', \'\', $exclude );
$attachments = get_children( array(\'post_parent\' => $id, \'exclude\' => $exclude, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
} else {
$attachments = get_children( array(\'post_parent\' => $id, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
}
if ( empty($attachments) )
return \'\';
if ( is_feed() ) {
$output = "\\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? \'right\' : \'left\';
$selector = "gallery-{$instance}";
$gallery_style = $gallery_div = \'\';
if ( apply_filters( \'use_default_gallery_style\', true ) )
$gallery_style = "
<style type=\'text/css\'>
#{$selector} {
margin: auto;
}
#{$selector} .gallery-item {
float: {$float};
margin-top: 10px;
text-align: center;
width: {$itemwidth}%;
}
#{$selector} img {
border: 2px solid #cfcfcf;
}
#{$selector} .gallery-caption {
margin-left: 0;
}
</style>
<!-- see gallery_shortcode() in wp-includes/media.php -->";
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id=\'$selector\' class=\'gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}\'>";
$output = apply_filters( \'gallery_style\', $gallery_style . "\\n\\t\\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$output .= "<{$itemtag} class=\'gallery-item\'>";
$output .= "
<{$icontag} class=\'gallery-icon\'>
$link
</{$icontag}>";
/* ORIGINAL CODE DISABLED
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class=\'wp-caption-text gallery-caption\'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= \'<br style="clear: both" />\';
}
$output .= "
<br style=\'clear: both;\' />
</div>\\n";
return $output;}
*/ // end disable original code
// REPLACEMENT CODE added by sol to allow line breaks in the captions
if ( $captiontag && trim($attachment->post_excerpt) ) {
$new_captiontag = str_replace("|", "<br />", $captiontag);
$output .= "
<{$new_captiontag} class=\'wp-caption-text gallery-caption\'>
" . wptexturize($attachment->post_excerpt) . "
</{$new_captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= \'<br style="clear: both" />\';
}
$output .= "
<br style=\'clear: both;\' />
</div>\\n";
return $output;}