Apply custom or random images to the featured pages


Note :This snippet works only with the Classical style option. The Classical style option can be enabled in Appearance > Customize > Advanced > Theme Style. More about the theme style option here


By default in the Customizr theme, the featured pages images use by priority :

  1. the page featured images
  2. if the featured image is not set, then it uses the first image (ordered by date) attached to this page. (in other words, the first images that has been inserted in the WYSIWYG editor).

This snippet describes how to take control over the images displayed in the home featured pages block of the theme.

customizr-featured-pages

 

 

Even if the final plugin function is very simple to use, this snippet uses quite advanced php coding techniques. Before diving into the code or simply pasting the code into your functions.php file, you might want to make sure you understand the following php and WordPress coding concepts :

PHP :

 

WordPress

 

How to use the snippet

The snippet is fired by a function named fp_random_images() which can take up to 4 optional arguments :

  • type (string) : ‘attachment’ or ‘placeholder’. Default : ‘placeholder’.
    • ‘attachement’ : list of images from your media library
    • ‘placeholder’ : images from http://lorempixel.com/
  • images source options (string or array). Default : array(). This parameter depends on the first one :
    • if ‘attachement’ is choosen, then you have to provide an array of image’s id from your media library
    • if ‘placeholder’ is choosen, then you can set an array of image categories among the following : ‘abstract’ , ‘animals’ , ‘business’ , ‘cats’, ‘city’, ‘food’, ‘nightlife’ , ‘fashion’, ‘people’, ‘nature’, ‘sports’, ‘technics’ , ‘transport’. Note: if no category is choosen, the plugin will display image taken from randomly choosen categories
  • color (boolean). Default : true. Displays the image in color if true, in grayscale if false. Note : this parameter works only with the placeholder type.
  • random( boolean). Default : true. Works with the attachement type only.

 

Examples of use

Example 1 : nature random placeholder images

Note : parameter #3 set to false will display the placeholder images in greyscale.

Example 2 : attachments from your media library

Note that the parameter #4 (random) is set to false and will replace the featured pages images in the same order of the array.

 

Commented source code

 

 

That’s it!

I hope you’ll enjoy this piece of code and I look forward to reading your comments and improvements!


Related Snippets you may also like


We can help you to implement this snippet

Coding is our job and we know WordPress inside and out. We can help you to customize your website with small chunks of code snippets like this one.

Send us your request and we'll get back to you quickly.



One thought on “Apply custom or random images to the featured pages

  • William Rodgers

    Thanks,
    That’s excellent and has many applications.
    In my case I’d just like to change the default image that is shown if once can;t be found – no featured image or no suitable image in the post.
    With some events I take as feeds and feature via FPU any images in the Event are from the host site so none suitable can be found and I get the default grey image suggesting an image size which I can’t set. I only want to change it for “featured pages” like that since I feature other pages and custom posts that are posted directly on the website and I can set a featured image for those.
    I admit to not being up on php (most of my programming is with MATLAB).
    can the above code be modified to suit my need or do I need something else?
    Thanks!

Comments are closed.