Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I use the post_gallery filter in functions.php to replace the core gallery code, with the gallery code I've modified to suit my needs, like this:

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);

Now, I am pretty sure that this line in the code is responsible for linking gallery thumbnails to their Attachment Page or full-size image:

$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);

How should I modify the line so that the thumbnails don't link to anything at all? (i.e. no <a href=""> around thumbnails in gallery)

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Instead of using 2 separate functions to grab the attachment you can use the same function, and then add the URL in separately using wp_get_attachment_url, making the logic much clearer, and reducing the amount of work needed:

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>";
}
share|improve this answer
add comment

Replace this line in your filtered gallery code:

$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:

$link = wp_get_attachment_image($id, $size);

Your gallery images are no longer hyperlinked -- i.e. they are linked to neither the attachment page nor the attachment file.


ALTERNATIVES

Replace:

$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); }

OR

Replace:

$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 );

Now, the just mentioned two methods allow you to use the link="none" parameter when you don't want the thumbnails in a specific gallery to be hyperlinked. (EDIT: Take a look at Tom's answer, he's provided some better code.)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.