/** * Add term/default image to blog/archive pages. Use: * add_action( 'genesis_entry_content', 'display_featured_image_genesis_add_archive_thumbnails', 5 ); * @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() { $show_thumbs = genesis_get_option('content_archive_thumbnail'); if (is_singular() || is_admin() || is_404() || !$show_thumbs) { return; } $args = array('post_mime_type' => 'image', 'post_parent' => get_the_ID(), 'post_type' => 'attachment'); $attached_images = get_children($args); if (has_post_thumbnail() || $attached_images) { return; } $image_id = display_featured_image_genesis_get_term_image_id(); if (empty($image_id)) { $image_id = display_featured_image_genesis_get_cpt_image_id(); if (empty($image_id)) { $image_id = display_featured_image_genesis_get_default_image_id(); } } if (empty($image_id)) { return; } $image = genesis_get_image(array('fallback' => apply_filters('display_featured_image_genesis_fallback_archive_thumbnail', $image_id), 'size' => genesis_get_option('image_size'), 'attr' => genesis_parse_attr('entry-image', array('alt' => get_the_title())), 'context' => 'archive')); $permalink = get_permalink(); printf('<a href="%1$s" aria-hidden="true">%2$s</a>', esc_url($permalink), wp_kses_post($image)); }
/** * Check the post content for the featured image. If it's there (full size), reset the featured image to a fallback. * @param $image_id * * @return string * @since 2.5.0 */ static function singular_reset_image($image_id) { $fullsize = wp_get_attachment_image_src($image_id, 'full'); $post = get_post(get_the_ID()); $item_content = strpos($post->post_content, 'src="' . $fullsize[0]); if (false === $item_content) { return $image_id; } $term_image = display_featured_image_genesis_get_term_image_id(); $default_image = display_featured_image_genesis_get_default_image_id(); // reset backstretch image source to term image if it exists and the featured image is being used in content. if ($term_image) { $image_id = $term_image; } elseif ($default_image) { // else, reset backstretch image source to fallback. $image_id = $default_image; } return $image_id; }
/** * retrieve image ID for output * @param string $image_id variable, ID of featured image * * @since 2.2.1 */ public static function set_image_id($image_id = '') { $frontpage = get_option('show_on_front'); // either 'posts' or 'page' $postspage = get_option('page_for_posts'); $displaysetting = get_option('displayfeaturedimagegenesis'); $postspage_image = get_post_thumbnail_id($postspage); $fallback = $displaysetting['default']; $medium = (int) get_option('medium_size_w'); $fallback_id = displayfeaturedimagegenesis_check_image_id($fallback); /** * create a filter to use the fallback image * @var filter * @since 2.0.0 (deprecated old use_fallback_image function from 1.2.2) */ $use_fallback = apply_filters('display_featured_image_genesis_use_default', array()); // set here with fallback preemptively, if it exists if (!empty($fallback)) { $image_id = $fallback_id; if (in_array(get_post_type(), $use_fallback)) { return (int) $image_id; } } // outlier: if it's a home page with a static front page, and there is a featured image set on the home page // also provisionally sets featured image for posts, similar to CPT archives if (is_home() && 'page' === $frontpage && !empty($postspage_image) || 'post' === get_post_type()) { $image_id = $postspage_image ? $postspage_image : $image_id; } $object = get_queried_object(); // singular or archive CPT if ($object && is_main_query() && !is_admin()) { $post_type = ''; if ($object->name) { // results in post type on cpt archive $post_type = $object->name; } elseif ($object->taxonomy) { // on a tax/term/category $tax_object = get_taxonomy($object->taxonomy); $post_type = $tax_object->object_type[0]; } elseif ($object->post_type) { // on singular $post_type = $object->post_type; } if (!empty($displaysetting['post_type'][$post_type])) { $image_id = displayfeaturedimagegenesis_check_image_id($displaysetting['post_type'][$post_type]); /** * use the custom post type featured image * * @since 2.2.1 */ $use_cpt = apply_filters('displayfeaturedimagegenesis_use_post_type_image', array()); if (in_array(get_post_type(), $use_cpt)) { return (int) $image_id; } } } if (is_author()) { $image_id = get_the_author_meta('displayfeaturedimagegenesis', (int) get_query_var('author')); } // taxonomy if (is_category() || is_tag() || is_tax()) { $t_id = $object->term_id; $term_meta = get_option("displayfeaturedimagegenesis_{$t_id}"); // if there is a term image if (!empty($term_meta['term_image'])) { $image_id = displayfeaturedimagegenesis_check_image_id($term_meta['term_image']); } } // any singular post/page/CPT if (is_singular()) { $term_image = display_featured_image_genesis_get_term_image_id(); if (!empty($term_image)) { $image_id = $term_image; /** * create filter to use taxonomy image if single post doesn't have a thumbnail, but one of its terms does. * @var filter */ $use_tax_image = apply_filters('display_featured_image_genesis_use_taxonomy', array()); if (in_array(get_post_type(), $use_tax_image)) { return (int) $image_id; } } $thumb_metadata = wp_get_attachment_metadata(get_post_thumbnail_id(get_the_ID())); // needed only for the next line $width = $thumb_metadata ? $thumb_metadata['width'] : ''; if (has_post_thumbnail() && $width >= $medium) { $image_id = get_post_thumbnail_id(get_the_ID()); } } /** * filter to use a different image id * @var $image_id * * @since 2.2.0 */ $image_id = apply_filters('display_featured_image_genesis_image_id', $image_id); // make sure the image id is an integer $image_id = is_numeric($image_id) ? (int) $image_id : ''; return $image_id; }
/** * If there is no image to use for the post thumbnail in archives, * optionally use the term or post type image as a fallback instead. * * @param $defaults * * @return mixed * @since 2.5.0 */ public function change_thumbnail_fallback($defaults) { if (!isset($this->setting['thumbnails']) || !$this->setting['thumbnails']) { return $defaults; } remove_action('genesis_entry_content', 'display_featured_image_genesis_add_archive_thumbnails', 5); $args = array('post_mime_type' => 'image', 'post_parent' => get_the_ID(), 'post_type' => 'attachment'); $attached_images = get_children($args); if ($attached_images) { return $defaults; } $image_id = display_featured_image_genesis_get_term_image_id(); if (empty($image_id)) { $image_id = display_featured_image_genesis_get_cpt_image_id(); } if ($image_id) { $defaults['fallback'] = $image_id; } return $defaults; }