• Resolved elainewildash

    (@elainewildash)


    Hi,

    Is there any way to define multiple featured images that can be used in posts and have them assigned randomly?

    Thanks.

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

    (@janwoostendorp)

    Hi Elaine,

    Yes this is possible, with a bit of custom code.
    In your wp-content/plugins/ folder create a file named: dfi-random.php
    With the following code:

    <?php
    /**
     * Plugin Name: Default Featured Image - Random post image
     * Plugin URI:  https://www.ads-software.com/support/topic/multiple-featured-images-6/
     * Version:     1.0
     */
    
    add_filter( 'dfi_thumbnail_id', 'dfi_random_image', 10, 2 );
    function dfi_random_image( $dfi_id, $post_id ) {
    	if ( get_post_type( $post_id ) !== 'post' ) {
    		return $dfi_id; // This is not a post, we only check posts.
    	}
    	$random_image_ids = array(
    		123, # Add your image id's in this array.
    		456,
    	);
    
    	return $random_image_ids[ array_rand( $random_image_ids ) ];
    }
    

    Replace the example image ids (123, 456,) with your own image ids.
    You can find the image ids in the media library when you open an image and check the url.

    After you change the id’s don’t forget to activate the plugin.

    If you need any help let me know.
    Jan-Willem

    Thread Starter elainewildash

    (@elainewildash)

    Thankyou. Much appreciated.

    CineWars

    (@max1138)

    So, does this go in the wp-contents/plugins/ folder or in the wp-contents/plugins/default-featured-image/ folder?

    And in my case, please tell me if I have done it correctly:

    <?php
    /**
     * Plugin Name: Default Featured Image - Random post image
     * Plugin URI:  https://www.ads-software.com/support/topic/multiple-featured-images-6/
     * Version:     1.0
     */
    
    add_filter( 'dfi_thumbnail_id', 'dfi_random_image', 10, 2 );
    function dfi_random_image( $dfi_id, $post_id ) {
    	if ( get_post_type( $post_id ) !== 'post' ) {
    		return $dfi_id; // This is not a post, we only check posts.
    	}
    	$random_image_ids = array(
    		206952, # Add your image id's in this array.
    		206951,
            206950,
            206949,
            206948,
            206947,
            206946,
            206945,
            206942,
            206941,
    	);
    
    	return $random_image_ids[ array_rand( $random_image_ids ) ];
    }
    CineWars

    (@max1138)

    Ack, trial and error, it’s just a wp-contents/plugins/ folder

    Works like a charm!

    ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Multiple Featured Images’ is closed to new replies.