Skip to content

Instantly share code, notes, and snippets.

@robincornett
Last active October 17, 2015 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robincornett/eeacc07fbe77b5de851d to your computer and use it in GitHub Desktop.
Save robincornett/eeacc07fbe77b5de851d to your computer and use it in GitHub Desktop.
Modify image/output options for Genesis archives, both globally and at a more specific level
<?php
// Do not include the opening tag!
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;
}
<?php
// Do not include the opening tag!
/**
* 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment