From 55592f130306a842ca9982340a3cdfd1eda26cf2 Mon Sep 17 00:00:00 2001 From: Robin Cornett Date: Thu, 26 Feb 2015 12:10:28 -0500 Subject: [PATCH] add more helper functions Functions to retrieve term image ID, URL, fallback image ID, URL, and function to display term/fallback image as post thumbnail on archives. --- includes/helper-functions.php | 85 ++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/includes/helper-functions.php b/includes/helper-functions.php index 6b8701c..27683b7 100644 --- a/includes/helper-functions.php +++ b/includes/helper-functions.php @@ -15,7 +15,7 @@ * * @since 2.1.0 */ -function display_featured_image_genesis_get_term_image() { +function display_featured_image_genesis_get_term_image_id() { $taxonomies = get_taxonomies(); $args = array( 'orderby' => 'count', 'order' => 'DESC' ); $terms = wp_get_object_terms( get_the_ID(), $taxonomies, $args ); @@ -33,3 +33,86 @@ function display_featured_image_genesis_get_term_image() { return $image_id; } + +/** + * Helper function to get the term image URL. + * @param string $size image size + * @return url URL associated with the term image + * + * @since 2.1.0 + */ +function display_featured_image_genesis_get_term_image_url( $size='displayfeaturedimage_backstretch' ) { + + $image_id = display_featured_image_genesis_get_term_image_id(); + + $image_url = wp_get_attachment_image_src( $image_id, $size ); + + return $image_url[0]; + +} + +/** + * Helper function to get the default image ID. + * @return ID ID associated with the fallback/default image + * + * @since 2.1.0 + */ +function display_featured_image_genesis_get_default_image_id() { + + $item = Display_Featured_Image_Genesis_Common::get_image_variables(); + + $image_id = Display_Featured_Image_Genesis_Common::get_image_id( $item->fallback ); + + return $image_id; + +} + +/** + * Helper function to get the default image URL. + * @param image size $size image size to retrieve + * @return URL URL associated with the term image + * + * @since 2.1.0 + */ +function display_featured_image_genesis_get_default_image_url( $size='displayfeaturedimage_backstretch' ) { + + $image_id = display_featured_image_genesis_get_default_image_id(); + + $image_url = wp_get_attachment_image_src( $image_id, $size ); + + return $image_url[0]; + +} + +/** + * Add term/default image to blog/archive pages. + * @return image If a post doesn't have its own thumbnail, you can use this function to add one to archive pages. + * + * @since 2.1.0 + */ +function display_featured_image_genesis_add_archive_thumbnails() { + + if ( ! is_home() && ! is_archive() && ! is_page_template( 'page_blog.php' ) ) { + return; + } + + if ( has_post_thumbnail() ) { + return; + } + + $size = genesis_get_option( 'image_size' ); + $image_url = display_featured_image_genesis_get_term_image_url( $size ); + if ( empty( $image_url ) ) { + $image_url = display_featured_image_genesis_get_default_image_url( $size ); + } + + if ( empty( $image_url ) ) { + return; + } + + $permalink = get_the_permalink(); + $alignment = genesis_get_option( 'image_alignment' ); + $image = ''; + echo $image; + +}