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]
Nick the Geek says
This is a good solution for automatically inserting the image into all posts the same way. This is the only solution if someone wishes to show the entire post content with the featured image via the widget or in the content archives. Generally this isn’t required and should not be built into the framework, since it can eb easily done via a child theme and limits flexibility within the framework if built in.
Linh Nguyen says
Hi I used the widget on my homepage and the featured image cannot display inline with the post. I don’t know why. Please help.
Travis Smith says
Hi, please use the StudioPress Support forums, and when you do, always include a link to your site.
sheila says
Hi,
I’m putting an agentpress website together for a friend –
1. First – how can I choose which featured images are shown in the featured widget section – it now takes the most recently entered listings and shows those – the only control I have is how many.
2. For the genesis slider – it takes the featured image and shows that – for the pictures of homes where I can’t get the picture big enough – what code can I alter or enter to have each picture in the slider centered.
3. Last question! I like the default color blue. Where can I override the background color – not on the inner section but around the inner section – when I enter the color in the background it is changed to the default.
Thanks!!
Alexis Villegas says
Thank you so much for this tip. After a few hours researching how to fix the problem I stumble upon your site and got the solution. Mu problem was that I just wanted to feature image to display on top of the post and I successfully got it by just using this part of the code above:
add_action(‘genesis_before_post_content’,’add_featured_image’);
add_image_size(‘post-image’, 700, 389, TRUE);
function add_featured_image(){
if (is_single() && (get_post_type() != ‘attachment’)) {
genesis_image(“format=html&size=post-image&attr=class= aligncenter”);
}
}
So my question now is, how do I modify this code to do the same in pages? I tried the following but it didn’t work:
add_action(‘genesis_before_post_content’,’add_featured_image’);
add_image_size(‘post-image’, 700, 389, TRUE);
function add_featured_image(){
if (is_single() && is_page && (get_post_type() != ‘attachment’)) {
genesis_image(“format=html&size=post-image&attr=class= aligncenter”);
}
}
Any help on how to add the is_page value to the if () statement would be really appreciated. Thanks!