I have a bunch of Posts with [gallery]s in them. They are in category "Galleries".

I want the category view to show (some||all) of the contents of the galleries in each post. Instead it seems to be stripping out or not rendering the gallery at all. Ideally I'd like to be able to control home much of the gallery is excerpted, but anything will do, really.

This seems like it would be a common request, but i can't find anything on it. Help and thanks!


The problem is the [gallery] shortcode (just like any other shortcode) is displayed when the_content() is used to display the post content, and normally themes do this in single template.

In themes, normally, insted of print the content is used the_excerpt() that strips out html and shortcodes.

Sure you can edit your category archive to use the_content() instead of the_excerpt(), but in this case all the post content is printed in the archive.

But it seems to me that you want to use the gallery as excerpt (show the gallery shortcode instead of the standard the_excerpt()).

Now, your category template (category.php) should contain something like that (semplified):

while( have_posts() ) :  the_posts();



So you can use 'the_excerpt' filter to output the shortcode when in galleries archive and when some other codition is reached (e.g. for first X number of posts).

add_filter('the_excerpt', 'maybe_excerpt_gallery');

function maybe_excerpt_gallery($excerpt) {
  global $post, $wp_query;
  if ( is_category() && in_category('galleries', $post) && $wp_query->current_post < 5 ) {
    $gallery = get_the_post_gallery_shortcode($post);
    return $gallery ? $gallery : $excerpt;

Some themes, use something like the_content( 'Continue reading...' ) in archives, in this case add the same filter to the_content: once the function check if we are on 'galleries' category archive, the_content function in other parts in theme will not be affected.

Before you go searching codex for get_the_post_gallery_shortcode function, I have to advice you that... this function does not exists in WordPress, but let's write it:

function get_the_post_gallery_shortcode( $post ) {
  if ( empty($post) ) global $post;
  if ( empty($post) || ! isset($post->post_content) ) return false;
  if (
    preg_match_all( '/'. get_shortcode_regex() .'/s', $post->post_content, $matches )
    && array_key_exists( 2, $matches ) && in_array( 'gallery', $matches[2] )
  ) {
    foreach ( $matches[2] as $i => $sc ) {
      if ( $sc == 'gallery' ) return do_shortcode( $matches[0][$i] );
  return false;

I've used the get_shortcode_regex to check and extract the shortcode (code for great part stolen from codex) and output it if found.

Now when you visit your galleries category archive, the first 5 posts will have the gallery (if exists) as excerpt .

| improve this answer | |
  • Thanks! - It is always nice when you think "Golly, somebody should write an article about this", you ask your question on SO, and somebody writes that article 15 minutes later. – Trass Vasston Sep 22 '13 at 2:29
  • 1
    You are welcome @TrassVasston. Let me know if it works, I've not tested ;) – gmazzap Sep 22 '13 at 2:32
  • 1
    Works great - I stuck the code near the header, modified the category name and systems go. For some reason a style was set in the theme to hide the image anyway, so beware of that. – Trass Vasston Sep 22 '13 at 2:40
  • 1
    good place for this code should be functions.php... leave templates as clean as possible is always a good idea. @TrassVasston – gmazzap Sep 22 '13 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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