您可以删除默认的短代码并创建自己的短代码。就像这样(在functions.php中):
remove_shortcode( \'gallery\' );
function my_own_gallary() {
// Gallery code
}
add_shortcode( \'gallery\' , \'my_own_gallary\' );
更改短代码的最简单方法是将其复制粘贴到函数中。php并将函数名称更改为
my_own_gallary
然后开始编辑。
正如goldenapples在评论中指出的那样:库短代码有一个过滤器,因此无需先删除短代码。
可以在函数中使用的示例。php(输出是默认的库短代码,所以您可以修改它)。
function my_own_gallery($output, $attr) {
global $post;
static $instance = 0;
$instance++;
// We\'re trusting author input, so let\'s at least make sure it looks like a valid orderby statement
if ( isset( $attr[\'orderby\'] ) ) {
$attr[\'orderby\'] = sanitize_sql_orderby( $attr[\'orderby\'] );
if ( !$attr[\'orderby\'] )
unset( $attr[\'orderby\'] );
}
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}>";
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;
}
add_filter("post_gallery", "my_own_gallery",10,2);
解释:在WordPress定义的快捷码中,您将看到:
$output = apply_filters(\'post_gallery\', \'\', $attr);
if ( $output != \'\' )
return $output;
这意味着,如果应用了过滤器并返回了一些内容,那么将使用这些内容(返回),否则函数将继续(默认短代码)。
要添加过滤器,请使用add_filter 作用第一个参数是过滤器的标记(在本例中为“post\\u gallery”),第二个参数是要添加的函数(将返回自定义库输出的函数)。
因此,这将为短代码[库]输出“test”:
function my_own_gallery($output, $attr) {
return \'test\';
}
add_filter("post_gallery", "my_own_gallery",10,2);
在我下面的示例“编辑”中,您将看到add\\u过滤器,它使用您自己的可编辑代码创建默认的短代码。你可以编辑这个,也可以从头开始。
<小时>(EDIT by Otto: 上述问题现已由奥托解决@RobVermeer缺少筛选器的第一个参数,并且没有正确执行add\\u筛选器。post\\u gallery过滤器是执行此操作的正确方法。删除短代码并重新添加(正如下面badlearner尝试的那样)是不可取的,因为它与修改库的其他插件也不兼容。)
添加/编辑人badlearner:使用函数修改库代码的过滤方法(在/wp-includes/media.php中)。@RobVermeer提供的php似乎工作不正常(请参阅此答案的注释)。
但是@RobVermeer的第一个答案(即,在第一次编辑之前)起到了作用,该答案取消了画廊短代码的注册并注册了一个新的画廊短代码。这是代码Please feel free to edit or add an answer if there\'s a better way.
以下是需要添加到主题函数中的代码。php文件:
<?php
remove_shortcode( \'gallery\' );
add_shortcode( \'gallery\' , \'my_own_gallary\' );
function my_own_gallary($attr) {
global $post;
static $instance = 0;
$instance++;
// Allow plugins/themes to override the default gallery template.
$output = apply_filters(\'post_gallery\', \'\', $attr);
if ( $output != \'\' )
return $output;
// We\'re trusting author input, so let\'s at least make sure it looks like a valid orderby statement
if ( isset( $attr[\'orderby\'] ) ) {
$attr[\'orderby\'] = sanitize_sql_orderby( $attr[\'orderby\'] );
if ( !$attr[\'orderby\'] )
unset( $attr[\'orderby\'] );
}
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} .gallery-item {
width: {$itemwidth}%;
}
</style>";
$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}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= \'\';
}
$output .= "
<div style=\'clear:both;\'></div>
</div>\\n";
return $output;
}
?>