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.

when I create a gallery I get this code generated yet I can;t find out how

<img width="150" height="150" src="http://localhost/wp2/wp-content/uploads/2012/06/28_0120-150x150.jpg" class="attachment-thumbnail" alt="_28_0120" title="_28_0120">

What function generates the thumbnail URL and how does the thumbnail image generated and cropped to the size of 150-150?

I forgot to mention:
for the gallery page I use get_children, so this is what I need to work with

share|improve this question
    
@AahanKrish how can I make my theme override it? –  ilyo Jun 29 '12 at 8:54
add comment

2 Answers

up vote 2 down vote accepted

The thumbnail size is based on the dimensions defined in WordPress Dashboard > Settings > Media, unless your theme overrides it.

how can I make my theme override it?

To change default post-thumbnail size, you need to use set_post_thumbnail_size. Add this in your functions.php file:

if ( function_exists( 'add_theme_support' ) ) {
    add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( W, H, true ); //change to false to disable hard cropping
}

Change W and H to the dimensions (in pixels) you want to set for the thumbnail. For example, set_post_thumbnail_size( 150, 150 ); sets your thumbnail size to 150 x 150 pixels.

NOTE: Check if your theme already has add_theme_support( 'post-thumbnails' ); — If it does, simply add set_post_thumbnail_size( W, H ); below it.


is there no other function specifically for the gallery, since the gallery thumbnail size I need is different from the post thumbnails.

OPTION - 1: (All the code goes into functions.php)

First, lets register new image size for the gallery thumbnails:

if ( function_exists( 'add_image_size' ) ) {

    // set gallery thumbnail image's size to 250 x 250 pixels
    add_image_size( 'ilyad-gallery-thumb', 250, 250, true ); //change to false to disable hard cropping

}

Now, lets modify the default WordPress gallery code with our own (so that we can set our own thumbnail size for images in gallery) — All the code below is the default gallery shortcode taken from wp-includes/media.php. I changed 'size' => 'thumbnail' to 'size' => 'gallery-thumb' to have the gallery use our newly registered image size for galleries.

// Modified Gallery Shortcode

add_filter("post_gallery", "wpse56909_post_gallery",10,2);
function wpse56909_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'    => 'dl',
        'icontag'    => 'dt',
        'captiontag' => 'dd',
        'columns'    => 3,
        'size'       => 'gallery-thumb',
        '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;
}

These changes do not affect your old posts. If you think it should, you need to regenerate the thumbnails for all posts, for which this is the most sought out plugin — AJAX Thumbnail Rebuild.

[Source]

OPTION - 2: (All the code goes into functions.php)

Lets say you want the size of thumbnails in gallery to be 250 x 250 pixels. Go to WordPress Dashboard > Settings > Media and set the Thumbnail size width and height to 250 and 250 respectively. Half done!

Now, we need to modify the size of thumbnails that represent your posts. Lets first register a new thumbnail size:

if ( function_exists( 'add_image_size' ) ) { 
    add_image_size( 'ilyad-post-thumb', 150, 150, true ); //change to false to disable hard cropping
}

Now all instances of code for post thumbnails in your theme files should be modified to use ilyad-post-thumb size. Here are two examples.

  • CASE - 1: If your theme uses something like this:

    <?php
    if ( '' != get_the_post_thumbnail() ) :
            $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), thumbnail );
    ?>
    <div class="entry-image">
        <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'reddle' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
            <img class="featured-image" src="<?php echo $thumbnail[0]; ?>" alt="">
        </a>
    </div>
    

    It should be modified to this:

    <?php
    if ( '' != get_the_post_thumbnail() ) :
            $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), ilyad-post-thumb );
    ?>
    <div class="entry-image">
        <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'reddle' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
            <img class="featured-image" src="<?php echo $thumbnail[0]; ?>" alt="">
        </a>
    </div>
    

    Above, we are simply changing an instance of thumbnail (image size) to ilyad-post-thumb.

  • CASE - 2: If your theme uses something like this:

    <?php 
    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
      the_post_thumbnail();
    } 
    ?>
    

    Change it to this:

    <?php 
    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
      the_post_thumbnail('ilyad-post-thumb');
    } 
    ?>
    

Those two examples should give you an idea as to how you should get going. :)

PS: This answer is written within the scope of my knowledge.

share|improve this answer
    
I already use post-thumbnails for, well.. post thumbnails :) is there no other function specifically for the gallery, since the gallery thumbnail size I need is different from the post thumbnails. –  ilyo Jun 29 '12 at 9:42
    
thank you for the detailed answer –  ilyo Jul 1 '12 at 9:19
add comment

In Wp-admin you can sett the size in Settings -> Media.

If you want the url on the page you can use this function:

<?php wp_get_attachment_thumb_url( $post_id ); ?>
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.