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'm still learning Wordpress and slowly getting better, but my skill set is not up to par for the changes I am asking for.

I need to customize the gallery output for two sections on my site: 1) Portfolio Pages and 2) Blog Posts

On the PORTFOLIO PAGES, I want WP to render the gallery as images tags with attributes as shown below.

<img width="575" height="500" src="ImagePath/ImageName.jpg" class="attachment-full" alt="Alt Text">
<img width="575" height="500" src="ImagePath/ImageName.jpg" class="attachment-full" alt="Alt Text">
<img width="575" height="500" src="ImagePath/ImageName.jpg" class="attachment-full" alt="Alt Text">
<img width="575" height="500" src="ImagePath/ImageName.jpg" class="attachment-full" alt="Alt Text">

Here's the code I found online that I modified. I'm not fully comprehending the $output section and not confident in modifying some of it's code. I don't need the "class='gallery-item" code but not sure what else I "don't" need. Do I need the "itemtag", "icontag", "captiontag", etc? I'm not using them but not sure if I can just delete. If I do, will they default back to the core defaults?

// ----------------------------------------------------------------
// GALLERY OUTPUT FOR PORTFOLIO PAGES

add_shortcode( 'gallery', 'modified_gallery_shortcode' );
function modified_gallery_shortcode($attr) {

if(is_page_template('page-portfolio.php')){ // EDIT this slug
    $attr['size']="full";
    $attr['link']="none";
    $attr['itemtag']="li";
    $attr['icontag']="span";
    $attr['captiontag']="p";

    $output = gallery_shortcode($attr);

    $output =strip_tags($output,'<img>');
    $output =str_replace(array(" class='gallery-item'"),array(""),$output);

}else{
    $output = gallery_shortcode($attr);
}
return $output; 
}

add_filter( 'use_default_gallery_style', '__return_false' );

On the BLOG POSTS, I want WP to render the gallery using an unordered list with the attributed as shown below.

<ul class="gallery">
<li><a href='ImagePath/ImageName.jpg' class="fancybox" rel="group" title='Title Text'><img width="150" height="150" src="ImagePath/ImageName.jpg" class="attachment-thumbnail" alt="Alt Text"></a></li>
<li><a href='ImagePath/ImageName.jpg' class="fancybox" rel="group" title='Title Text'><img width="150" height="150" src="ImagePath/ImageName.jpg" class="attachment-thumbnail" alt="Alt Text"></a></li>
<li><a href='ImagePath/ImageName.jpg' class="fancybox" rel="group" title='Title Text'><img width="150" height="150" src="ImagePath/ImageName.jpg" class="attachment-thumbnail" alt="Alt Text"></a></li>
</ul>

I tried modifying the "Gallery Output for Portfolio Pages to accommodate the formatting requirements for this post type, but not having any luck!

// ----------------------------------------------------------------
// GALLERY OUTPUT FOR BLOG POSTS

add_shortcode( 'gallery2', 'modified_gallery_shortcode2' );
function modified_gallery_shortcode2($attr) {

if(is_page_template('page-portfolio.php')){ // EDIT this slug
    $attr['size']="full";
    $attr['link']="file";
    $attr['itemtag']="li";
    $attr['icontag']="span";
    $attr['captiontag']="p";

    $output = gallery_shortcode($attr);

    $output =strip_tags($output,'NOT SURE HOW TO FORMAT');
    $output =str_replace(array(" class='gallery-item'"),array(""),$output);

}else{
    $output = gallery_shortcode($attr);
}
return $output; 
}

add_filter( 'use_default_gallery_style', '__return_false' );

ANY HELP IS VERY MUCH APPRECIATED! Thank you!

share|improve this question

1 Answer 1

Just use the regular gallery shortcode, and replace the rendering function (the callback) very late, in wp_head:

add_action( 'wp_head', function() 
{
    if ( 'post' === get_post_type() )
        return add_shortcode( 'gallery', 'modified_gallery_shortcode2' );

    if ( 'portfolio' === get_post_type() )
        return add_shortcode( 'gallery', 'modified_gallery_shortcode' );
});
share|improve this answer
    
Thank Toscho! This is definitely an option rather than creating two gallery functions and I will use it. But I'm still after the answer to my question on modifying the output of the code I added above. BTW, when you said edit in the "wp-head"... do you mean add it in the functions.php file? –  Adrian Oct 14 '13 at 23:05

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.