/******************************************************************************************* SCRIPT INFO: Description: Category-based random header images for the Graphene theme Author: Francisco de Azevedo (a.k.a., Marventus) (based on code by Syahir Hakim) License: Share at will (with attribution) PLEASE DO NOT DELETE THIS INFO ********************************************************************************************/ /******************************************************************************************** INSTRUCTIONS: A. What It Does: This script allows you to display custom header images for specific category archive pages and single posts in the Graphene Theme based on your category hierarchy. It supports up to 3 levels of depth. B. How to Use It: In order for the script to work, you need to: 1. Create a folder called "custom-headers" inside the folder "graphene/images/"; 2. Using the category slugs as folder names, create a directory tree inside that folder that mirrors the category hierarchy of the categories for which you wish to display custom images; 3. Upload images (.JPG only) to those folders; and 4. Insert all LEVEL 1 (i.e. main) category IDs you wish to target as a comma- separated list inside the $image_cats array below. There is no need to include any LEVEL 2 or LEVEL 3 categories (i.e., sub-categories) in the array: the script will retrieve custom images for all sub-categories under the targeted LEVEL 1 categories. C. How It Works: The script checks if the current archive or post category is targeted in the array, and if it is, it checks if a custom image folder exists for that cat and if contains images. If it does, it wil fetch them and displays a random one. If it does not, it will fetch the images from the LEVEL 1 category and display a random one. D. An Example: Let's suppose you have the following category structure. The cat ID of each category is shown in parenthesis. A star symbol (*) next to a main category means I am targeting that category (i.e., I have included it in the array). An at symbol (@) next to any category means I would like to display custom images for that category: People (2) @ * - Family (12) @ -- Close Family (41) @ -- Extended Family (39) - Friends (15) -- School (30) @ -- Work (32) Objects (5) @ * - Tools (10) @ -- Utensils (25) @ -- Computers (27) Places (7) * Hobbies (8) Based on that, my $image_cats array would be defined as such: $image_cats = array(2,5,7); I would have to create the following directories inside "grapehne/images" and upload images to each one: /custom-headers/ /custom-headers/people/ /custom-headers/people/family/ /custom-headers/people/family/close-family/ /custom-headers/people/friends/ /custom-headers/people/friends/school/ /custom-headers/objects/ /custom-headers/objects/tools/ /custom-headers/objects/tools/utensils/ Now, while navigating through the site, here's what I'll see inside each category: - Inside People | Images from /people/; - Inside People -> Family | Images from /people/family/; - Inside People -> Family -> Close Family | Images from /people/family/close-family/; - Inside People -> Family -> Extended Family | Images from /people/; - Inside People -> Friends | Images from /people/; - Inside People -> Friends -> School | Images from /people/friends/school/; - Inside People -> Friends -> Work | Images from /people/; - Inside Places (or its sub-cats) | Default images, because, even though the category is included in the $image_cats array, there is no folder /custom-headers/places/; - Inside Hobbies (or its sub-cats) | Default images. YOI CAN DELETE THESE INSTRUCTIONS OR SAVE THEM TO A TEXT FILE IF DESIRED ********************************************************************************************/ /******************************************************************************************** SCRIPT START ********************************************************************************************/ function custom_header_image($random_image) { /* CATEGORIES FOR CUSTOM IMAGES */ $image_cats = array(4,6,8,9); // modify at will /* GET CURRENT CAT AND INFO */ // Test if we are inside a cat archive or a post if ( is_category() || is_single() ){ // Test if we are inside a cat archive if (is_category() ) { // If we are, get cat and cat info $current_cat_id = get_query_var( 'cat' ); $current_cat_array = get_category($current_cat_id); $current_cat_slug = $current_cat_array->slug; $parent_cat_id = $current_cat_array->parent; // If we are not, we are inside a single post } else { // Get cat and cat info global $post; $current_cat_array = get_the_category( $post->ID ); $current_cat_id = $current_cat_array[0]->cat_ID; $current_cat_slug = $current_cat_array[0]->slug; $parent_cat_id = $current_cat_array[0]->parent; } // Get the rest of the info $parent_cat_array = get_category($parent_cat_id); $parent_cat_slug = $parent_cat_array->slug; $ancestor_cat_id = $parent_cat_array->parent; $ancestor_cat_array = get_category($ancestor_cat_id); $ancestor_cat_slug = $ancestor_cat_array->slug; /* GET CUSTOM IMAGES FOR CATS */ // Check if we are dealing with one of the selected categories if ( in_array( $current_cat_id, $image_cats ) || in_array( $parent_cat_id, $image_cats )|| in_array( $ancestor_cat_id, $image_cats ) ) { // If we are, put together the absolute path $current_cat_slug_slash = $current_cat_slug . '/'; $parent_cat_slug_slash = $parent_cat_slug . '/'; $ancestor_cat_slug_slash = $ancestor_cat_slug . '/'; $image_start_path = get_stylesheet_directory() . '/images/custom-headers/'; $image_start_dir = get_stylesheet_directory_uri() . '/images/custom-headers/'; $custom_images = ''; // Check which directory to retrieve the custom images from and save them in a variable if ( $ancestor_cat_slug ) { $image_abs_path = $image_start_path . $ancestor_cat_slug_slash . $parent_cat_slug_slash . $current_cat_slug_slash; if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) { $image_dir = $image_start_dir . $ancestor_cat_slug_slash . $parent_cat_slug_slash . $current_cat_slug_slash; } else { $image_abs_path = $image_start_path . $ancestor_cat_slug_slash; $image_dir = $image_start_dir . $ancestor_cat_slug_slash; $custom_images = glob( $image_abs_path . "/*.jpg" ); } } elseif ( !$ancestor_cat_slug && $parent_cat_slug ) { $image_abs_path = $image_start_path . $parent_cat_slug_slash . $current_cat_slug_slash; if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) { $image_dir = $image_start_dir . $parent_cat_slug_slash . $current_cat_slug_slash; } else { $image_abs_path = $image_start_path . $parent_cat_slug_slash; $image_dir = $image_start_dir . $parent_cat_slug_slash; $custom_images = glob( $image_abs_path . "/*.jpg" ); } } elseif ( $current_cat_slug && !$parent_cat_slug ) { $image_abs_path = $image_start_path . $current_cat_slug_slash; if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) { $image_dir = $image_start_dir . $current_cat_slug_slash; } } // Retrieve random images from custom image obtained if ( $custom_images ) { $key = array_rand( $custom_images ); $random_image = $image_dir . basename( $custom_images[$key] ); } } } // Return the random image obtained return $random_image; } add_filter( 'graphene_header_image', 'custom_header_image' ); /******************************************************************************************** SCRIPT END ********************************************************************************************/