Site icon WP Smith

StudioPress's Genesis Featured Post Glitch with Show Content

Recently in working with the Featured Posts Widget for my sites (and then seeing this post in the StudioPress forums), I've learned that there is a "glitch" in its code or in the use of the widget. Either way, the code should be updated to protect against user error.

Scenario: User creates a post. Inserts an image into the post so that when it appears as a single post page, the image appears as the user desires. Then they set that same image as the featured image. So let's say that they set the image on the first line as a class of alignleft.

Now if they were to select to show the content (and show content limit may also cause problems depending on where the image is and how it gets pulled into the_content();), as in all of the content, then that's where the problem arises.

The featured posts shows the content which includes the image that is set in the post. So now there are two images showing as can be seen here.

Furthermore, there are several widgets out there that extend the Genesis Featured Post widget (or vice versa).

Solution: But the problem can be prevented in two ways.

First, as a band-aid of sorts, there are two solutions that we can use as users without hacking core, because we should never hack core. First, is always use Show Content Limit and set accordingly (trial & error) or use Excerpt. Second, do not insert the picture on your post and add this code to your functions.php file, which will place the image at the top of the post aligned to the left. If you want to align it to the right simply change alignleft to alignright or aligncenter to align center.

[php]add_action('genesis_before_post_content','add_featured_image');
add_image_size('Medium 16x9', 400, 225, TRUE);
function add_featured_image(){
if (is_single() && (get_post_type() != 'attachment')) {
genesis_image("format=html&size=Medium 16x9&attr=class=alignleft");
}
}[/php]

Medium 16x9 can be changed to whatever title description you'd like. If you change the description in add_image_size, then change it in genensis_image. And the dimensions can be changed to whatever you'd like where width = 400 and height = 225. genesis_add_image_size($name, $width = 0, $height = 0, $crop = FALSE); is used but is also deprecated probably because WordPress has image_size( $name, $width = 0, $height = 0, $crop = FALSE);.

Second, the solution can be solved by StudioPress in the core of Genesis by changing the code in featured-post-widget.php at about line 58 (and probably featured-page-widget.php at about line 44). The original code has:

[php]if(!empty($instance['show_image'])) :
printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute('echo=0'), esc_attr( $instance['image_alignment'] ), genesis_get_image( array( 'format' => 'html', 'size' => $instance['image_size'] ) ) );
endif;
[/php]

And it could be changed to the following so it doesn't show the image to eliminate user confusion. While it doesn't have to be changed, it is the most user-friendly approach.

[php]if(!empty($instance['show_image'])) :
if(!empty($instance['show_content'])) :
if($instance['show_content'] == 'content') :
printf( '<a href="%s" class="%s" title="%s">%s</a>', get_permalink(), the_title_attribute('echo=0'), esc_attr( $instance['image_alignment'] ), genesis_get_image( array( 'format' => 'html', 'size' => $instance['image_size'] ) ) );
endif;
endif;
endif;
[/php]