Harness the Power of Genesis Archive Options

About a month ago, I decided to make some minor adjustments to my website, primarily because I wanted to manage my plugins better. Since my last website update, for example, I’ve started selling SuperSide Me (using Easy Digital Downloads), and I was not happy at all with how that page looked. The ensuing adventure resulted in a complete overhaul to make the CSS mobile-first, a revamp of two existing plugins I’d written for myself, and an entirely new plugin.

Change Archive Options on a Global Level

Genesis Archive Options

So, if you are a Genesis Framework (affiliate link) user, you’re already familiar with your Genesis Settings page, where you can set up the content limit and featured image options for your entire site (pictured). This affects every archive page–not only posts, but also custom content types, such as my downloads and portfolio. So, it’s a good start, but only a start, because of course I want to change it up.

Change Archive Options for a Custom Content Type

One change I made was to move my WordPress hosted plugins to the Downloads section of the website, even though they still have to be downloaded from wordpress.org. But bringing all of the plugins into one place was a bit sticky–I still obviously want SuperSide Me to be the most important plugin on that list, for instance. For now, at least, I can make sure it’s listed first because it’s the most recent one posted, but I want to set it off visually as well, with a larger image. I also want my downloads page to look different than the regular blog pages. A little thumbnail just won’t cut it.

However, if I change the Archive Options, I’ll be messing with my posts, and I don’t want that. So how can I change things for just Downloads? Turns out, I can handle that with the 'genesis_options' filter, applied conditionally to any EDD archive (this could be in your functions.php file, although I’ve made a separate file in my theme folder to handle this):

add_filter( 'genesis_options', 'leaven_downloads_archive_options' ); /** * Change number of posts, as well as size and image alignment of featured image * on EDD custom post type */ function leaven_downloads_archive_options( $args ) { if ( ! leaven_is_content_type_archive( 'download' ) ) { return $args; } global $wp_query; $args['content_archive_thumbnail'] = 1; $args['image_size'] = 'medium'; $args['image_alignment'] = 'aligncenter'; $args['content_archive'] = 'excerpts'; if ( 0 === $wp_query->current_post ) { $args['image_size'] = 'large'; } return $args; } function leaven_is_content_type_archive( $post_type ) { $terms = get_object_taxonomies( $post_type ); if ( is_post_type_archive( $post_type ) || is_tax( $terms ) ) { return true; } return false; }
Code language: PHP (php)

Note what I’ve changed:

  1. 'content_archive_thumbnail': set to 1 (or true), which I’ve done in case I remove the featured image from the global options.
  2. 'image_size': set to medium instead of thumbnail
  3. 'image_alignment': set to center. One interesting thing to mention here is that centering the featured image is not an option in the Genesis Theme Settings at all, so the only way to manage that is with a filter.
  4. 'content_archive': set to excerpts, because I’m doing those by hand for each download.
  5. 'image_size' pops up again, this time for the first post (SuperSide Me) in the archive, because I want that to have a larger image. If I decide to sell another premium plugin, this is going to have to change, but for the moment, it does just what I want it to.

So, now my download archives behave just as I want them to (note that I’ve made other changes to it not documented here, such as laying out the repository plugins in a grid instead of being full width).

Change Archive Options Based on Another Criteria

Let’s go back to my posts, and look at an issue which I know is not unique to me. Often, when a theme changes, new image sizes are introduced. Old image sizes may disappear completely because they’re no longer needed. If you want to use a new image size in your template, you can, and WordPress does its best to accommodate you, and it generally does it fairly well. If it’s incredibly important to you to have all of your images updated to the new size, you can use a plugin like Regenerate Thumbnails to go back and make new, properly sized copies of old images.

If, however, you’re lazy like me, or you know that some older images won’t handle the new image size well, you might want to take a more targeted approach. The plugin allows you to pick and choose which images you update, but what about the pesky Archive Options? What if your old images won’t work with the layout which matches the new images? Here’s what I want to accomplish: I want to use my new large image size for all new posts, and certain older posts, but not for posts which have featured images which might look odd:

Featured Image Archive Options

The essential part of this is that I want to use my new featured image size if and only if it actually exists. Since WordPress will always return an image for me, even if the specific size doesn’t exist, because it’s being helpful, how can I check for that actual size? WordPress lets me check that by using the wp_get_attachment_image_src function, which does this:

Returns an ordered array with values corresponding to the (0) url, (1) width, (2) height, and (3) scale of an image attachment (or an icon representing any attachment).

It’s usually used to get the URL of an image, but what I’m looking for today is that final bit, the scale. It’s a bit confusing as far as the terminology goes, but specifically, it returns a “boolean: true if $url is a resized image, false if it is the original or if no image is available.” So if I use this function, I can check against that last value. Even if it returns an image (which it generally will), it turns out that if a copy of the image at my newly created specific size does not exist, that value will be false. And that’s really helpful!

The code I used for the Downloads can’t get quite this granular (at least it wouldn’t for me), so I needed another option. I found it in another Genesis filter: "genesis_pre_get_option_{$key}". This kind of filter always blows my mind just a little bit, so I needed to think it through. The key (literally) is the {$key} part. This means that I can change that to a specific key from the larger options array and target just that.

Let’s post the code and then I’ll unpack it a bit:

/** * Add new image size for blog featured images */ add_image_size( 'featured_blog', 960, 320, true ); /** * Helper function: check if a copy of the image has been created at a specific size * @param {string} $image_size registered image size * @return {boolean} true only if an actual copy of the image exists at this size */ function leaven_does_image_size_exist( $image_size ) { $image_id = get_post_thumbnail_id(); $image_src = wp_get_attachment_image_src( $image_id, $image_size ); if ( isset( $image_src ) && $image_src[3] ) { return true; } return false; } add_filter( 'genesis_pre_get_option_image_size', 'leaven_modify_blog_images' ); /** * Change the image size used for blog posts on archives * @param {$image_size} which registered image size to use * @return {string} use the featured blog image size if it exists */ function leaven_modify_blog_images( $image_size ) { if ( leaven_does_image_size_exist( 'featured_blog' ) ) { return 'featured_blog'; } return $image_size; } add_filter( 'genesis_pre_get_option_image_alignment', 'leaven_modify_blog_image_alignment' ); /** * Change the alignment for featured images, only if the new size exists * @return {string} $alignment alignment for image */ function leaven_modify_blog_image_alignment( $alignment ) { if ( leaven_does_image_size_exist( 'featured_blog' ) ) { return 'aligncenter'; } return $alignment; }
Code language: PHP (php)

The first function adds the new image size, featured_blog, to my WordPress site. It’s quite wide, but not tall, so some of my older images will be ugly if they’re cropped that way.

Next, I’ve created a helper function: leaven_does_image_size_exist. I’m doing it this way because it’s enough code that I would prefer to type it out just once, and who knows? I might want to use this kind of helpful check somewhere else in my theme. So the image size has to be passed in as an argument, in case I want to use it in a different context. The function returns either a true or false value, based on whether that final key (scale) returns true or false.

Now that I have my helpful helper function ready, I can come back to the Genesis filters, and I create two, one for the image_size, and one for the image_alignment. Since my posts will use the thumbnail by default, I’ve set that in my overall Genesis Archive Options, and for them to be aligned to the right. I’m not going to change that here at all because I want the freedom to change the Theme Settings (for example, if I want to align the images to the left, or use the medium size instead). So, the two filters simply check if the featured image for the post has a copy at the new size. If it does, then the new size is used, and the alignment is set to center. If it does not, the default set in the Theme Settings is used instead.

It’s a fair bit of thinking for what feels like rather a short bit of code, but the end result is exactly what I want. Now, if I want to go back and regenerate older images, I can do that absolutely at my leisure. If I don’t want to change the older images, I can leave them be and the layout will be just fine. It’s a nice way to get a new look while maintaining some backwards compatibility.

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.