WordPress.org

Ready to get started?Download WordPress

Forums

Default featured image
[resolved] Post Category (22 posts)

  1. JeffreySummers
    Member
    Posted 1 year ago #

    How about the ability to set it for each post category individually?

    http://wordpress.org/extend/plugins/default-featured-image/

  2. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    You can do it in your functions.php like this:

    add_action('template_redirect', function () {
      if ( has_category('category-slug') ) {
        add_filter('dfi_thumbnail_id', function () { return 12; //media id } );
      } else if( has_category('diffrent-category-slug') ) {
        add_filter('dfi_thumbnail_id', function () { return 200; //media id } );
      }
    });

    You probably are wondering why you can't set it on the category (edit) page. It's a lot more complicated, a post can have multiple categories, how would I chose (for you) which category I should pick? And should a parent overrule a child or the other way around?

    There will have to be some logic behind it. And I'm sure my choices won't make sense to everybody. The code isn't that hard.

    I'm not saying I'll never implement it but I don't know when and how. Probably I'll do post-types first.

  3. JeffreySummers
    Member
    Posted 1 year ago #

    Appreciate the response. I see what you're saying about multiple categories and I would simply choose the main category as you suggest. I know just enough code to get me into trouble so I'll see if I can find someone to do that part of it before I mess it up.

    Thanks again. Appreciate the follow-up very much.

    Jeffrey

  4. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    Let me know if you can't figure it out.

  5. JeffreySummers
    Member
    Posted 1 year ago #

    I can't figure it out. = )

  6. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    Straight forward answer :)

    1) Did you paste my code above in functions.php of your theme?
    2) In my code there is a 'category-slug' change that to the slug of your categories, you can find that on the category pages.
    3) This way add all categories you need, in the example there are two to add more duplicate the } else if( line and the one below that.
    4) in the return 12; //media id replace the number with the media id. You can find the media id on a media page, look at the url It will be something like: wp-admin/post.php?post=694&action=edit use that number.

    Give it a try and let me know ;)

  7. JeffreySummers
    Member
    Posted 1 year ago #

    Have 26 categories to build - will try. Where in the function.php does it go? Matter?

  8. JeffreySummers
    Member
    Posted 1 year ago #

    This is what I got...

    add_action('template_redirect', function () {
    if ( has_category('10-things') ) {
    add_filter('dfi_thumbnail_id', function () { return 17158; //media id } );
    } else if( has_category('culinary') ) {
    add_filter('dfi_thumbnail_id', function () { return 17155; //media id } );
    } else if( has_category('foh') ) {
    add_filter('dfi_thumbnail_id', function () { return 17149; //media id } );
    } else if( has_category('holiday-posts') ) {
    add_filter('dfi_thumbnail_id', function () { return 17159; //media id } );
    } else if( has_category('hotel-notes') ) {
    add_filter('dfi_thumbnail_id', function () { return 17157; //media id } );
    } else if( has_category('travel-tuesday') ) {
    add_filter('dfi_thumbnail_id', function () { return 17147; //media id } );
    } else if( has_category('how-we-see-it') ) {
    add_filter('dfi_thumbnail_id', function () { return 17107; //media id } );
    } else if( has_category('coach') ) {
    add_filter('dfi_thumbnail_id', function () { return 16137; //media id } );
    } else if( has_category('consult') ) {
    add_filter('dfi_thumbnail_id', function () { return 17160; //media id } );
    } else if( has_category('podcasts') ) {
    add_filter('dfi_thumbnail_id', function () { return 17148; //media id } );
    } else if( has_category('pub-speak-facilitate') ) {
    add_filter('dfi_thumbnail_id', function () { return 16785; //media id } );
    } else if( has_category('qfo') ) {
    add_filter('dfi_thumbnail_id', function () { return 16789; //media id } );
    } else if( has_category('they-just-dont-get-it') ) {
    add_filter('dfi_thumbnail_id', function () { return 17153; //media id } );
    } else if( has_category('marketing-branding') ) {
    add_filter('dfi_thumbnail_id', function () { return 17151; //media id } );
    } else if( has_category('groupon') ) {
    add_filter('dfi_thumbnail_id', function () { return 17146; //media id } );
    } else if( has_category('guest-experience') ) {
    add_filter('dfi_thumbnail_id', function () { return 17143; //media id } );
    } else if( has_category('social-business') ) {
    add_filter('dfi_thumbnail_id', function () { return 17142; //media id } );
    } else if( has_category('surveys-research') ) {
    add_filter('dfi_thumbnail_id', function () { return 17145; //media id } );
    } else if( has_category('yelp') ) {
    add_filter('dfi_thumbnail_id', function () { return 17144; //media id } );
    } else if( has_category('people-culture-hwsi') ) {
    add_filter('dfi_thumbnail_id', function () { return 16759; //media id } );
    } else if( has_category('tlntmgmt') ) {
    add_filter('dfi_thumbnail_id', function () { return 17150; //media id } );
    } else if( has_category('train') ) {
    add_filter('dfi_thumbnail_id', function () { return 16742; //media id } );
    } else if( has_category('restaurant-notes') ) {
    add_filter('dfi_thumbnail_id', function () { return 17161; //media id } );
    } else if( has_category('business-2') ) {
    add_filter('dfi_thumbnail_id', function () { return 16782; //media id } );
    } else if( has_category('nso') ) {
    add_filter('dfi_thumbnail_id', function () { return 17064; //media id } );
    } else if( has_category('the-law') ) {
    add_filter('dfi_thumbnail_id', function () { return 17156; //media id } );
    } else if( has_category('govt-regs') ) {
    add_filter('dfi_thumbnail_id', function () { return 17156; //media id } );
    } else if( has_category('leadership-theory') ) {
    add_filter('dfi_thumbnail_id', function () { return 17154; //media id } );
    } else if( has_category('cost-controls-hwsi') ) {
    add_filter('dfi_thumbnail_id', function () { return 16476; //media id } );
    } else if( has_category('technology-2') ) {
    add_filter('dfi_thumbnail_id', function () { return 17162; //media id } );
    }
    });

    Look ok?

  9. JeffreySummers
    Member
    Posted 1 year ago #

    I inserted in into the functions.php at the end...made the entire website go white. So I replaced the original file until you give me further instructions.

  10. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    It's my bad :( the // media id is replaced with /*media id*/

    add_action('template_redirect', function () {
    if ( has_category('10-things') ) {
    add_filter('dfi_thumbnail_id', function () { return 17158; /*media id*/ } );
    } else if( has_category('culinary') ) {
    add_filter('dfi_thumbnail_id', function () { return 17155; /*media id*/ } );
    } else if( has_category('foh') ) {
    add_filter('dfi_thumbnail_id', function () { return 17149; /*media id*/ } );
    } else if( has_category('holiday-posts') ) {
    add_filter('dfi_thumbnail_id', function () { return 17159; /*media id*/ } );
    } else if( has_category('hotel-notes') ) {
    add_filter('dfi_thumbnail_id', function () { return 17157; /*media id*/ } );
    } else if( has_category('travel-tuesday') ) {
    add_filter('dfi_thumbnail_id', function () { return 17147; /*media id*/ } );
    } else if( has_category('how-we-see-it') ) {
    add_filter('dfi_thumbnail_id', function () { return 17107; /*media id*/ } );
    } else if( has_category('coach') ) {
    add_filter('dfi_thumbnail_id', function () { return 16137; /*media id*/ } );
    } else if( has_category('consult') ) {
    add_filter('dfi_thumbnail_id', function () { return 17160; /*media id*/ } );
    } else if( has_category('podcasts') ) {
    add_filter('dfi_thumbnail_id', function () { return 17148; /*media id*/ } );
    } else if( has_category('pub-speak-facilitate') ) {
    add_filter('dfi_thumbnail_id', function () { return 16785; /*media id*/ } );
    } else if( has_category('qfo') ) {
    add_filter('dfi_thumbnail_id', function () { return 16789; /*media id*/ } );
    } else if( has_category('they-just-dont-get-it') ) {
    add_filter('dfi_thumbnail_id', function () { return 17153; /*media id*/ } );
    } else if( has_category('marketing-branding') ) {
    add_filter('dfi_thumbnail_id', function () { return 17151; /*media id*/ } );
    } else if( has_category('groupon') ) {
    add_filter('dfi_thumbnail_id', function () { return 17146; /*media id*/ } );
    } else if( has_category('guest-experience') ) {
    add_filter('dfi_thumbnail_id', function () { return 17143; /*media id*/ } );
    } else if( has_category('social-business') ) {
    add_filter('dfi_thumbnail_id', function () { return 17142; /*media id*/ } );
    } else if( has_category('surveys-research') ) {
    add_filter('dfi_thumbnail_id', function () { return 17145; /*media id*/ } );
    } else if( has_category('yelp') ) {
    add_filter('dfi_thumbnail_id', function () { return 17144; /*media id*/ } );
    } else if( has_category('people-culture-hwsi') ) {
    add_filter('dfi_thumbnail_id', function () { return 16759; /*media id*/ } );
    } else if( has_category('tlntmgmt') ) {
    add_filter('dfi_thumbnail_id', function () { return 17150; /*media id*/ } );
    } else if( has_category('train') ) {
    add_filter('dfi_thumbnail_id', function () { return 16742; /*media id*/ } );
    } else if( has_category('restaurant-notes') ) {
    add_filter('dfi_thumbnail_id', function () { return 17161; /*media id*/ } );
    } else if( has_category('business-2') ) {
    add_filter('dfi_thumbnail_id', function () { return 16782; /*media id*/ } );
    } else if( has_category('nso') ) {
    add_filter('dfi_thumbnail_id', function () { return 17064; /*media id*/ } );
    } else if( has_category('the-law') ) {
    add_filter('dfi_thumbnail_id', function () { return 17156; /*media id*/ } );
    } else if( has_category('govt-regs') ) {
    add_filter('dfi_thumbnail_id', function () { return 17156; /*media id*/ } );
    } else if( has_category('leadership-theory') ) {
    add_filter('dfi_thumbnail_id', function () { return 17154; /*media id*/ } );
    } else if( has_category('cost-controls-hwsi') ) {
    add_filter('dfi_thumbnail_id', function () { return 16476; /*media id*/ } );
    } else if( has_category('technology-2') ) {
    add_filter('dfi_thumbnail_id', function () { return 17162; /*media id*/ } );
    }
    });

    The rest of your code is valid. This should work.

  11. JeffreySummers
    Member
    Posted 1 year ago #

    Same result - site goes totally white, have to replace functions.php page with original....???

  12. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    You need to add it to the functions.php. I know the code is correct. So the problem is probably somewhere in the functions.php. Can you share your whole functions.php I advise using http://pastebin.com/

  13. JeffreySummers
    Member
    Posted 1 year ago #

    I did add it. Got the white-out result. Here's the pastebin link.

    http://pastebin.com/8vSyZHkG

  14. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    its private, I cant see it

  15. JeffreySummers
    Member
    Posted 1 year ago #

    Should be able to now.

  16. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    http://pastebin.com/cTpJ2zFD

    An edited version which is error free. I suspect that you run an older version of php so I made the code to work with older versions.

    let me know ;)

  17. JeffreySummers
    Member
    Posted 1 year ago #

    This one didn't break the theme but didn't display any of the images either. = (

  18. JeffreySummers
    Member
    Posted 1 year ago #

    It did break the theme again..just took me a min top find it. Inhibited me from uploading new images. Unless you can figure this out, I think I'm done. If you ever develop this functionality in this plugin, please let me know. I'd buy it.

  19. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    Weird I have this code tested and it works.
    Somewhere my code is interfering with other code. And that is not something we can figure out here.
    :(

    If I ever do implement category images I'll let you know.

  20. leonjb
    Member
    Posted 1 year ago #

    The first code broke my theme as well. I wanted a category image, so that users who did not upload a pic had one automatically installed.. I installed the main plugin, but I need about 20 images connected to each category.. So I can run my site like a forum for members..

  21. leonjb
    Member
    Posted 1 year ago #

    It seems to lose its border as well, when its shown next to an excerpt, is there a way of fixing this.. My theme shows borders.. Can you point me in the right direct of the file to edit..

  22. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    Hi leonjb,

    For the category you should try the following in you functions.php:

    function dfi_category( $dfi_id ) {
      if ( has_category( 'category-slug' ) ) {
        return 13; // the image id
      } else if ( has_category( 'other_category' ) ) {
        return 14; // the image id
      }
      return $dfi_id; // the original featured image id
    }
    add_filter('dfi_thumbnail_id', 'dfi_category' );

    The borders are due to a bug, I made a new version. You should get an update notice later today. Plugin repository is giving me a hard time.

    For a manual update, download this zip remove the plugin, and install that zip. The already chosen DFI will be lost.

    So I recommend waiting a bit

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.