• Resolved jes88

    (@jam2988)


    Hello.

    Thank you for creating such a great plugin. It helped me a lot.

    I was just wondering if it’s possible to create multiple fallback featured images for each specific post category.

    Your help is greatly appreciated.

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Jan-Willem

    (@janwoostendorp)

    Hi Jess,

    Yes, its possible. there is a example in the FAQ. You can put the code in the theme’s functions.php or in a new/separate plugin.

    I’m not near a computer to provide a full example. If you need more help let me know and I’ll help tomorrow.

    Jan-Willem

    Thread Starter jes88

    (@jam2988)

    Hello Jan,

    Thanks for the reply.

    I have tried it, but it only shows one photo in a category. Is it possible to have a set of random photos for each category?

    Like 5 different dog photos for the “dog” category and 5 different cat photos for the “cat” category.

    I have tried to use it with your random featured image additional plugin, but I think it’s not compatible.

    Plugin Author Jan-Willem

    (@janwoostendorp)

    Hi jess,

    The following code should help. It allows you to set different images per category.

    add_filter( 'dfi_thumbnail_id', 'dfi_category_random', 10, 2 );
    function dfi_category_random( $dfi_id, $post_id ) {
        // Set a different image for posts that have the 'cats' category set.
        if ( has_category( 'cats', $post_id ) ) {
            $random_cats = array(
                7,
                8,
            );
            return $random_cats[ array_rand( $random_cats ) ]
        }
        // Set a different image for posts that have the 'dogs' category set.
        if ( has_category( 'dogs', $post_id ) ) {
            $random_dogs = array(
                10,
                14,
            );
            return $random_dogs[ array_rand( $random_dogs ) ]
        }
    
        return $dfi_id; // the original featured image id.
    }

    Let me know how it goes.

    Jan-Willem

    Thread Starter jes88

    (@jam2988)

    Hi Jan,

    I tried creating a new plugin, but it says, “Plugin could not be activated because it triggered a fatal error.” every time I activate it.

    Plugin Author Jan-Willem

    (@janwoostendorp)

    Yes sorry there is a syntax error, that’s what you get when doing it in a hurry..
    The code below has the missing ;

    add_filter( 'dfi_thumbnail_id', 'dfi_category_random', 10, 2 );
    function dfi_category_random( $dfi_id, $post_id ) {
        // Set a different image for posts that have the 'cats' category set.
        if ( has_category( 'cats', $post_id ) ) {
            $random_cats = array(
                7,
                8,
            );
            return $random_cats[ array_rand( $random_cats ) ];
        }
        // Set a different image for posts that have the 'dogs' category set.
        if ( has_category( 'dogs', $post_id ) ) {
            $random_dogs = array(
                10,
                14,
            );
            return $random_dogs[ array_rand( $random_dogs ) ];
        }
    
        return $dfi_id; // the original featured image id.
    }
    Thread Starter jes88

    (@jam2988)

    It’s working now.

    Thanks, Jan, for your help; I really do appreciate it!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Multiple images for different categories’ is closed to new replies.