/** * 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)); }
/** * Echo the widget content. * * @since 2.0.0 * * * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. * @param array $instance The settings for the particular instance of the widget */ function widget($args, $instance) { //* Merge with defaults $instance = wp_parse_args((array) $instance, $this->defaults); $post_type = get_post_type_object($instance['post_type']); $option = get_option('displayfeaturedimagegenesis'); if ('post' === $instance['post_type']) { $frontpage = get_option('show_on_front'); // either 'posts' or 'page' $postspage = get_option('page_for_posts'); $postspage_image = get_post_thumbnail_id($postspage); $title = get_post($postspage)->post_title; $permalink = esc_url(get_the_permalink($postspage)); if ('posts' === $frontpage || 'page' === $frontpage && !$postspage) { $postspage_image = display_featured_image_genesis_get_default_image_id(); $title = get_bloginfo('name'); $permalink = home_url(); } } else { $title = $post_type->label; $permalink = esc_url(get_post_type_archive_link($instance['post_type'])); if (post_type_supports($instance['post_type'], 'genesis-cpt-archives-settings')) { $headline = genesis_get_cpt_option('headline', $instance['post_type']); if (!empty($headline)) { $title = $headline; } } } echo $args['before_widget']; if (!empty($instance['title'])) { echo $args['before_title'] . apply_filters('widget_title', $instance['title'], $instance, $this->id_base) . $args['after_title']; } $image = ''; $image_id = 'post' === $instance['post_type'] ? $postspage_image : displayfeaturedimagegenesis_check_image_id($option['post_type'][$post_type->name]); $image_src = wp_get_attachment_image_src($image_id, $instance['image_size']); if ($image_src) { $image = '<img src="' . $image_src[0] . '" alt="' . $title . '" />'; } if ($instance['show_image'] && $image) { $role = empty($instance['show_title']) ? '' : 'aria-hidden="true"'; printf('<a href="%s" title="%s" class="%s" %s>%s</a>', esc_url($permalink), esc_html($title), esc_attr($instance['image_alignment']), esc_attr($role), $image); } if ($instance['show_title']) { if (!empty($instance['show_title'])) { $title_output = sprintf('<h2><a href="%s">%s</a></h2>', $permalink, esc_html($title)); if (genesis_html5()) { $title_output = sprintf('<h2 class="archive-title"><a href="%s">%s</a></h2>', $permalink, esc_html($title)); } echo wp_kses_post($title_output); } } $intro_text = ''; if (post_type_supports($instance['post_type'], 'genesis-cpt-archives-settings')) { $intro_text = genesis_get_cpt_option('intro_text', $instance['post_type']); } elseif ('post' === $instance['post_type']) { $intro_text = get_post($postspage)->post_excerpt; if ('posts' === $frontpage || 'page' === $frontpage && !$postspage) { $intro_text = get_bloginfo('description'); } } if ($instance['show_content'] && $intro_text) { echo genesis_html5() ? '<div class="archive-description">' : ''; $intro_text = apply_filters('display_featured_image_genesis_cpt_description', $intro_text); echo wp_kses_post(wpautop($intro_text)); echo genesis_html5() ? '</div>' : ''; } echo $args['after_widget']; }
/** * set and retreive variables for the featured image. * @return $item * * @since 1.1.0 */ public static function get_image_variables() { $item = new stdClass(); // variables internal to this function $displaysetting = get_option('displayfeaturedimagegenesis'); $fallback = $displaysetting['default']; // url only // sitewide variables used outside this function $item->backstretch = ''; // turn Photon off so we can get the correct image $photon_removed = ''; if (class_exists('Jetpack') && Jetpack::is_module_active('photon')) { $photon_removed = remove_filter('image_downsize', array(Jetpack_Photon::instance(), 'filter_image_downsize')); } /** * create a filter for user to optionally force post types to use the large image instead of backstretch * @var filter * * @since 2.0.0 */ $use_large_image = apply_filters('display_featured_image_genesis_use_large_image', array()); $image_size = 'displayfeaturedimage_backstretch'; if (in_array(get_post_type(), $use_large_image)) { $image_size = 'large'; } $image_id = self::set_image_id(); $item->backstretch = wp_get_attachment_image_src($image_id, $image_size); // set a content variable so backstretch doesn't show if full size image exists in post. $item->content = ''; // declare this last so that $item->backstretch is set. if (!is_admin() && is_singular()) { $fullsize = wp_get_attachment_image_src($image_id, 'full'); $post = get_post(); $item->content = strpos($post->post_content, 'src="' . $fullsize[0]); if (false !== $item->content) { $source_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 (!empty($term_image)) { $source_id = $term_image; } elseif (!empty($fallback)) { // else, reset backstretch image source to fallback. $source_id = $default_image; } $item->backstretch = wp_get_attachment_image_src($source_id, $image_size); $item->content = strpos($post->post_content, 'src="' . $item->backstretch[0]); } } // turn Photon back on if ($photon_removed) { add_filter('image_downsize', array(Jetpack_Photon::instance(), 'filter_image_downsize'), 10, 3); } // $title is set by new title function $title = self::set_item_title(); /** * Optional filter to change the title text * @since 2.2.0 */ $item->title = apply_filters('display_featured_image_genesis_title', $title); return $item; }
/** * 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; }