删除图库中的缩略图链接的正确方法

时间:2012-05-09 作者:its_me

我使用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=""> 围绕图库中的缩略图)

2 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

您可以使用相同的函数,而不是使用两个单独的函数来获取附件,然后使用wp_get_attachment_url, 使逻辑更加清晰,减少所需工作量:

e、 g。

$image = wp_get_attachment_image( $id, $size, false );

// if it\'s set to not show the image link
if(isset($attr[\'link\']) && (\'none\' == $attr[\'link\']) ){
    // then just show the image
    echo $image;
} else {
    // else show the image wrapped in a link
    $link = wp_get_attachment_url($id);
    echo "<a href=\\"$link\\">$image</a>";
}

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, 他提供了一些更好的代码。)

结束

相关推荐

Overriding Gallery Margin

继续构建免费主题。使用图库CSS。Wordpress会自动将左边距添加到库中。示例:http://themeforward.com/demo2/features/gallery-2/ 那么,我该如何摆脱这种令人讨厌的利润呢?/* Gallery */ .gallery { margin:30px auto auto; text-align:center } .gallery-item { float:left; margin-top:10px;&#x