If you follow my personal blog or me on twitter, then you know I decided to change how archives display Gallery post formats. Previously I had the full content showing for galleries and just included a [gallery] short tag. Mostly I like that, since people could view my images directly in the archive, but this wouldn’t work well if I uploaded more than say 9 images to the gallery (a 3×3 grid with my current setup). In fact, I was intentionally limiting myself on how many images I would post to a gallery because of that. When I was reading on post formats and saw how Matt Mullenweg was handling his galleries, well it just made sense.
I have started with the StudioPress Tapestry Child Theme and it is a very well done theme. So far most of my changes have been small tweaks to make the theme work more for me, personalization if you would. I’ll be documenting most of the changes I’ve made in these tutorials.
One of the big advantages to the Tapestry theme is the built in support for Post Formats, but out of the box it treats all post formats pretty much the same. Some of my changes have added small, but key differences in the various post formats. I can now show the featured image and content limit for my standard posts, but the full content for the asides, quotes, and other formats. This change displays the featured image and content limit with text describing how many attached items are in the gallery.
The code for this is pretty straight forward. First, I had to adapt the Tapestry code that handles all the actions for the post formats.
add_action( 'genesis_before_post', 'tapestry_remove_elements' ); /** * If post has post format, remove the title, post info, and post meta. * If post does not have post format, then it is a default post. Add * title, post info, and post meta back. * * @since 1.0 */ function tapestry_remove_elements() { // Setup Gallery Post Format if ( 'gallery' == get_post_format() && ! is_single() ) { remove_action( 'genesis_post_title', 'genesis_do_post_title' ); remove_action( 'genesis_before_post_content', 'genesis_post_info' ); remove_action( 'genesis_after_post_content', 'genesis_post_meta' ); remove_action( 'genesis_post_content', 'genesis_do_post_content' ); remove_action( 'genesis_post_content', 'the_content' ); add_action( 'genesis_post_content', 'genesis_do_post_image' ); add_action( 'genesis_post_content', 'ntg_do_gallery_post_content' ); } // Remove if post has format elseif ( get_post_format() && ! is_single() ) { remove_action( 'genesis_post_title', 'genesis_do_post_title' ); remove_action( 'genesis_before_post_content', 'genesis_post_info' ); remove_action( 'genesis_after_post_content', 'genesis_post_meta' ); remove_action( 'genesis_post_content', 'genesis_do_post_image' ); remove_action( 'genesis_post_content', 'genesis_do_post_content' ); add_action( 'genesis_post_content', 'the_content' ); remove_action( 'genesis_post_content', 'ntg_do_gallery_post_content' ); } // Add back, as post has no format else { add_action( 'genesis_post_title', 'genesis_do_post_title' ); add_action( 'genesis_before_post_content', 'genesis_post_info' ); add_action( 'genesis_after_post_content', 'genesis_post_meta' ); add_action( 'genesis_post_content', 'genesis_do_post_image' ); add_action( 'genesis_post_content', 'genesis_do_post_content' ); remove_action( 'genesis_post_content', 'the_content' ); remove_action( 'genesis_post_content', 'ntg_do_gallery_post_content' ); } }
If you have Tapestry you will see several differences in this code block. The special sauce for this can be found between lines 13 and 21.
Line 13 checks to see if it is working with a gallery post format and is not on a single post page. Then lines 14-18 remove the title, post info, post meta, post content, and the_content() (this is added for other post formats since I’m showing the full content). Line 19 adds the post image back in, since other post formats will remove that function. The real magic is line 20. This replaces the content with a new function “ntg_do_gallery_post_content().” That creates the text seen in the galleries on archives.
/** * Outputs Content for Galleries on archive pages * * @author Nick_theGeek * @link https://designsbynickthegeek.com/tutorials/changing-the-gallery-post-format-output */ function ntg_do_gallery_post_content() { global $post; $attachments = get_children( array( 'post_parent' => $post->ID ) ); $imgCount = count( $attachments ); the_content_limit( 400, ' ' ); echo '<p class="gallery-item-count">This album contains <a href="' . get_permalink() . '">' . $imgCount . ' items</a></p>'; }
Yep, that’s all there is to that magic function. Anytime you work with $post in a function you need to make it global (line 7). Line 9 fetches the attachments for the post and line 10 counts how many. Line 12 retrieves the content limit. For standard posts I’m limiting to 500 characters, but my extra paragraph means I need a limit of 400. I’ve also made the link ” ” because I want the ellipse added to the end, but no actual link text. Line 14 creates the last paragraph of text detailing how many images are in the gallery and linking to the post. I added a class to the
tag because I wanted to style it to align right.
You can see it in action on my personal blog. Here is a tag archive with a couple of galleries in it.
Christina says
Hi, I’m just learning how to pop the hood and customize wordpress/genesis. Could you tell me how to insert this into the functions.php?
nickthegeek says
Thanks for the comment, I don’t provide explicit support via the site. You can start a question in the StudioPress support forums. The code provided is a modification of existing code in the Tapestry Child theme, so for this to make sense you should be starting with that child theme.
Attorneys says
I wish i could write like you do… Great article publish.