Support » Plugin: Default featured image » Confused on Using Different Images on Categories

  • Resolved theredheadhenry

    (@theredheadhenry)


    Hello!

    A bit rusty with WordPress. Need some help with getting this snippet of code to work:

    function dfi_category ( $dfi_id, $post_id ) {
      // all which have 'animals' as a category
      if ( has_category( 'animals', $post_id ) ) {
    
        //sub category
        if ( has_category( 'cats', $post_id ) ) {
          return 7; // cats img
        } else if has_category( 'dogs', $post_id ) {
          return 8; // dogs img
        }
    
        return 6; // default animals picture
      }
      return $dfi_id; // the original featured image id
    }
    add_filter( 'dfi_thumbnail_id', 'dfi_category', 10, 2 );

    This is the code we have for the Use a different image for some categories, section of the FAQ. I’m trying to set multiple different images for different categories, so when I post something to Category A is pulls Image A. If I post something to category B, it pulls Image B. I understand the jist of what this code is doing, what I don’t understand is where specifically am I placing this code, is it the bottom of the functions page? And how am I linking the new images, am I listing out the URL of the image under return XYZ?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author janw.oostendorp

    (@janwoostendorp)

    Hello,

    you can post this code in your theme’s functions.php
    To set an image you need to have it in your media library in WordPress.
    And set the image ID. That’s why the return 7; is a number.

    You can find the number by going to the media library. opening the image you want. Then the image ID is in the url. Example: wp-admin/upload.php?item=922
    The 922 is the image ID.

    Does this make it clear for you?

    Let me know if you need more help.

    Jan-Willem

    Thread Starter theredheadhenry

    (@theredheadhenry)

    I actually got an error message trying to save this:

    function dfi_category ( $dfi_id, $post_id ) {
      // all which have 'cat1' as a category
      if ( has_category( 'cat1', $post_id ) ) {
    
        //sub category
        if ( has_category( 'cat2', $post_id ) ) {
          return 902; // cat2 img
        } else if has_category( 'cat3', $post_id ) {
          return 603; // cat3 img
        }
    
        return 667; // cat1 picture
      }
      return $dfi_id; // the original featured image id
    }
    add_filter( 'dfi_thumbnail_id', 'dfi_category', 10, 2 );
    

    Error message:

    Your PHP code changes were rolled back due to an error on line 153 of file /functions.php. Please fix and try saving again.

    syntax error, unexpected ‘has_category’ (T_STRING), expecting ‘(‘

    Plugin Author janw.oostendorp

    (@janwoostendorp)

    Hi There is a typo line:

    } else if has_category( 'cat3', $post_id ) {

    should be
    } else if ( has_category( 'cat3', $post_id ) ) {

    This is also a typo in the example (I’ll fix it there later)

    Thread Starter theredheadhenry

    (@theredheadhenry)

    Shouldn’t this gave a second closing )? It didn’t save without it, this is what I put:

    } else if ( has_category( 'cat3', $post_id ) ) {

    It seems to be working, unless this is some kind of fluke.

    Plugin Author janw.oostendorp

    (@janwoostendorp)

    Hello,

    Yes, this should work.
    Glad to hear it’s working.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Confused on Using Different Images on Categories’ is closed to new replies.