我使用post_gallery
filter 在函数中。php将核心库代码替换为我根据需要修改的库代码,如下所示:
function aahan_post_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\' => \'div\',
\'icontag\' => \'\',
\'captiontag\' => \'div\',
\'columns\' => 3,
\'size\' => \'full\',
\'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=\'myCarousel\' class=\'gallery-carousel slide\'><div class=\'carousel-inner\'>";
$output = apply_filters( \'gallery_style\', $gallery_style . "\\n\\t\\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$count++;
$aahan_active = (1 == $count) ? \' active\' : \'\';
$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=\'item$aahan_active\'>";
$output .= "$link";
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></div>\\n";
return $output;
}
add_filter("post_gallery", "aahan_post_gallery",10,2);
现在,我很确定代码中的这一行负责将库缩略图链接到其附件页或全尺寸图像:
$link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
我应该如何修改线条,使缩略图根本不链接到任何内容?(即编号
<a href="">
围绕图库中的缩略图)
SO网友:its_me
在筛选的库代码中替换此行:
$link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
使用此选项:
$link = wp_get_attachment_image($id, $size);
您的库图像不再是超链接的,即它们既不链接到附件页,也不链接到附件文件。
备选方案替换:
$link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
With this:
if(isset($attr[\'link\']) && \'file\' == $attr[\'link\']){ $link = wp_get_attachment_link($id, $size, false, false); } elseif (isset($attr[\'link\']) && \'none\' == $attr[\'link\']){$link = wp_get_attachment_image($id, $size, false);} else { $link = wp_get_attachment_link($id, $size, true, false); }
或更换:
$link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
With this:
$image = wp_get_attachment_image( $id, $size, false );
$link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$image_output = ( \'none\' == $attr[\'link\'] ? $image : $link );
现在,刚才提到的两种方法允许您使用
link="none"
不希望特定库中的缩略图超链接时的参数。(
EDIT: 看看
Tom\'s answer, 他提供了一些更好的代码。)