From the Blogabout me

Featured Images in Genesis – Hiding, Displaying and Removing them.

I’m a huge fan of Genesis and StudioPress as a whole.  Also, Brian Gardner is amazing at ideas and the work he has developed along the way.  Even so, there are occasions when I need to tweak things just a little to fit my  (my clients) needs.

One particular example is making sure the Featured Images of Posts are displaying in the correct places of Genesis. In this short tutorial I am going to display and explain the code for getting the Featured Images to fit your needs. Although I am going to be referring to Genesis code, most of this code can easily be converted to other themes, minus the Genesis calls. Also for simplicity reasons, I will refer to the Agency theme, but again use your Child Theme names where you see “agency” if it helps you.  Feel free to contact me if you have particular questions about the code.

Here are the scenarios:

I want Featured Images on Single Posts (individual Posts all by themselves and nothing else on the screen, or some themes will use single.php as the theme)

Open your Child Theme functions.php (remember we said we are using Agency)
/** Add Post image above post title, single posts only */
add_action( 'genesis_before_post', 'agency_post_image' );
function agency_post_image() {

if ( is_category() ) return;
if ( $image = genesis_get_image( 'format=url&size=post-image' ) ) {
printf( '<a href="%s" rel="bookmark"><img class="post-photo" src="%s" alt="%s" /></a>', get_permalink(), $image, the_title_attribute( 'echo=0' ) );
}
}

What’s going on here, you ask? So let’s go through this – line by line

add_action( 'genesis_before_post', 'agency_post_image' );
= We need to add an action for Genesis to know what to do, so using Genesis hook reference genesis_before_post is what executes before each post in all loop blocks (outside the post_class() div)

function agency_post_image() {
= This is just to start the new function that your aforementioned action will call. You can name it whatever you want to, just use it correctly in the above action as well.

if ( is_category() ) return;
= This code tells Genesis if you are on XYZ page (Category Pages, in this case) to just return nothing and not display anything special. This is the BIGGEST thing to remember, and you will see why shortly.

The last if statement is saying “When we display an image, here is the URL, the size of the image you uploaded, and I am going to print the URL of the original Post/Page the image was a Featured Image of.

Now this is where it gets real fun. Read on…

If you were to change is_category to something slightly different you would get an entirely different outcome, so:

if ( is_category() ) return;
= as originally explained, this makes sure Featured Images do not display on Category pages; but does show everywhere else.

if ( is_page() ) return;
= this makes sure Featured Images do not display on Pages; but does show everywhere else.

if ( is_single() ) return;
= this makes sure Featured Images do not display on Single Posts pages; but does show everywhere else.

Happy Coding

Comments

  1. This is what I looking for… with this little example.. we can made awesome and unique Genesis Theme

  2. You’re a live saver!

    What I needed was to display the feature pictures everywhere except the “Single Post” pages.

    I’ve used
    – add_action( ‘genesis_before_post_content’, ‘agency_post_image’ );

    and
    if ( is_single() ) return;

    works great thanks!

    – Nick

  3. Thanks for the code, Kevin. How would this be coded for HTML5, as this doesn’t work my newly converted Genesis site?

    • k.shoffner says:

      Glad to help out Grae. Can you let me know what theme you are using, as well as the version of Genesis you are currently using?

  4. Hey,

    I am wondering how to remove the featured image from SPECIFIC POSTS on the HOME PAGE in genesis.

    I’ve been searching for how to do this forever now and coming up blank. I only find results about how to remove it from specific categories.

    Thus far I’ve been able to remove the featured images INSIDE a bunch of old posts, but I want those very same posts’ featured images to be removed from the FRONT PAGE as well, not just from inside the posts. So I have gotten 50% of the job done.

    Here’s the code I used to do that in my php:

    /** Add post image above post title */
    add_action( ‘genesis_before_post’, ‘streamline_post_image’ );
    function streamline_post_image() {

    if ( is_single( array( 140, 145, 154, 91, 160, 161, 162, 173, 180, 206, 194, 213, 239, 291, 312, 323, 333, 382, 424, 493, 516, 562, 622, 659, 684, 717, 752, 791, 750, 816, 847, 862, 884, 889, 950, 969, 1030, 1015, 1057, 1086, 1105, 1159, 1185, 1228, 1246, 1257, 1263, 1295 ) ) ) return;
    if ( is_page ( 28 ) ) return;

    if ( $image = genesis_get_image( ‘format=url&size=post-image’ ) ) {
    printf( ‘‘, get_permalink(), $image, the_title_attribute( ‘echo=0’ ) );
    }

    }

    And then from genesis forums I got the suggestion to use this code to remove it on from the front page, but I need to change the conditional tag, and I am having issues doing that properly in this code:

    add_action(‘genesis_post_content’, ‘remove_featured_image_specific_category’);
    function remove_featured_image_specific_category() {
    if(is_category(’21’) ) {
    remove_action( ‘genesis_post_content’, ‘genesis_do_post_image’ );

    }

    }

    Can you make any sense of how to do this?

    • k.shoffner says:

      This is quick reply as I am mobile at the moment, but have you tried to use is_home or is_front_page yet?

      • Yes I have tried that a few times. But that has resulted in removing ALL of the featured images from Home, and I only want to remove the specifically chosen ones that I have already managed to remove from inside the posts.

        It is however entirely possible that I have been using “is_home” and “is_front_page” the wrong way though as I am a real beginner at CSS & PHP.

        The way I used those functionals were first in the:

        if ( is_home( array(……………..(insert IDs)…….) return;

        Then I tried it in the second command that I posted – the “remove function command” but I am failing there because I am not sure how change it..

        • k.shoffner says:

          I did some testing and troubleshooting while briefly this morning and one way I was able to make it work (although probably not the most efficient way), was to create a new category (something like “hide-image”) and tag all of the Posts that you didn’t want to show images, yet display the post itself on the homepage. Then I was able to do a keep my original category, as well as mark the post with “hide-image” and change my if_category array to include the hide-image category, and the image no longer appeared.

          Another possibility worth trying would be something like:

          Idea

          • Thanks for your effort.

            I manually copied the code you pasted above (please make it available for direct copy paste) and got this:

            Parse error: syntax error, unexpected ‘<', expecting ')' in /customers/6/5/b/startgainingmomentum.com/httpd.www/wp-content/themes/streamline/functions.php on line 34

            I do believe I have copied it correctly…?

            add_action( 'genesis_before_post_content', 'hide_post_image' );

            function hide_post_image() {
            if ( is_post( array( 140, 145, 154, 91, 160, 161, 162, 173, 180, 206, 194, 213, 239, 291, 312, 323, 333, 382, 424, 493, 516, 562, 622, 659, 684, 717, 752, 791, 750, 816, 847, 862, 884, 889, 950, 969, 1030, 1015, 1057, 1086, 1105, 1159, 1185, 1228, 1246, 1257, 1263, 1295 ) ) ) return;
            if ( $image = genesis_get_image( 'format=url&size=post-image') ) {
            printf( ‘, get_permalink(), $image, the_title_attribute( ‘echo=0’ ) );
            }

          • k.shoffner says:

            The code I used in the last post was done as an image. I am mobile again today; so if you can try the images code.

  5. Hey Kevin,

    Thanks so much for this post!

    I used the is_home() ) and is_single lines to remove the featured image exactly as I wanted.

    Except there’s one really weird issue. It seems that both on the homepage, and on the individual posts, the text is indented all the way to the far right. It’s almost as if WordPress expects a picture there and is blocking out the space just in case.

    It looks really odd, and I’d really like to change it back to normal. Do you know what’s going on here?

    My site is invigoratenow.com btw. Thanks again!

    • k.shoffner says:

      Sayan,

      Thanks. Trying going into your streamline CSS code, around like 740 and look for “.entry-content p”
      Once you are there, add “float: left;” (without quotes of course) and see if that works for you.

      • Hey Kevin,

        More weird stuff. I enter the float:left; and it worked great for the homepage. But then when I went to view individual posts, everything was jumbled. I’m talking like portions of paragraphs from the bottom of the page somehow overlapping the image. And headlines all out of whack. Really odd.

        Any idea what’s up with it? I’ll leave it “on” for now if you can take a look for a moment.

        Thanks.

        • k.shoffner says:

          For the individual posts, I would look at the code and how the plugins you are using (the Share via Social plugin especially) is overwriting some of your themes CSS. You might have to do some different CSS if you still plan to use the plugin.

          As for the headlines, I would just look again at making some adjustments to your you H2, wp-caption, .alignright, a combination of some/all of these. It’s just trying to determine where you want the post image, compared to your Heading. I would probably move down the Post image, so the heading could be the full width of my entry-content area.

          Good luck

  6. thank you so much for this code i am finding the solution for single post and i found there i am new wordpres and very helpful for me

  7. Thank you so much for this great tutorial, Kevin. I’m new to this Genesis thing but I instantly loved it!

    I have implemented this customization for my new blog but the featured image shows up on the homepage too. What I did was change the code:

    if ( is_category() ) return;

    to

    if ( is_category() || is_home() ) return;

    • I am glad you liked my tutorial.

      As for your homepage, I am curious. Have you gone into Widgets >> Home Middle (or where ever you have Genesis-Featured Post widget) and made sure you do not have the “Show Featured Image” checkbox selected?

      • The solution Luis provided works great for excerpts with a 150 x 150 image because it gets rid of the full sized featured image from showing above the image+excerpt.

        In the Genesis > Theme Settings > Content Archives you set ‘display post excerpts’ tic ‘include the featured image’ then set your size and position as desired.

  8. Hello there, what to do when featured images do not show up on single posts?

    I’m referring to http://kehitys.dexviihde.fi/?artisti=artisti-1

    • k.shoffner says:

      This image should appear by default, so make sure you have not edited anything which may have caused the issue. You can also go into the functions.php file and try to do something like:

      add_action( 'genesis_post_content', 'genesis_do_post_image' );

      As this should make sure the Post Image is displayed.

  9. Hi. How do I make the featured image appear below the title? Thanks! 🙂

    • You will need to go into your functions.php file and look for the genesis_post_title function. Depending on your child theme, you may have to do some extra work.
      Start by looking for:
      add_action( 'genesis_post_title','child_post_title(' );

      If you do not see it, you will need to do something more like:
      genesis_post_title
      or
      genesis_before_post_content
      depending on what you want to do the most with the image

      The rest of the steps are pretty much the same once you add/edit the above functions

      A great reference = http://my.studiopress.com/docs/hook-reference/

  10. Hi Kevin,

    I do not want the featured image to hyperlinked to post url. What should I do in this code?

    Thanks

  11. I’m looking for a way to NOT automatically set the first image as the featured image. I added support for my genesis theme to use featured images in both posts and pages, but there are times I don’t want a page to use a featured image, but still have images in the body of the page. I can’t seem to figure out how to do that. Any suggestions?

    • k.shoffner says:

      Probably the simplistic way of doing this, without a plugin or code adjustments, would be the set your actual “Featured Image” as a small transparent PNG, or whatever you want your Featured Image to actually be. This would then make that image, not your first image, the featured image.

  12. Any ideas how to remove the featured image from search results?

    • k.shoffner says:

      I’ve been able to do something like the below in the past for some clients within in CSS as a false fix:
      .imagect {display: none;}

      But you can also do something like the below function if you are more comfortable with making changes to the core or theme files:
      // Exclude images from search results - WordPress
      add_action( 'init', 'exclude_images_from_search_results' );
      function exclude_images_from_search_results() {
      global $wp_post_types;

      $wp_post_types['attachment']->exclude_from_search = true;
      }

Trackbacks

  1. […] also created a tutorial on how to display featured images on Category Post Pages also, so take a look over […]

Leave a Reply

%d bloggers like this: