Advertisement
Guest User

Category-based random header images for the Graphene theme

a guest
Nov 17th, 2011
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.04 KB | None | 0 0
  1. /*******************************************************************************************
  2.     SCRIPT INFO:
  3.    
  4.     Description: Category-based random header images for the Graphene theme
  5.     Author:      Francisco de Azevedo (a.k.a., Marventus)
  6.                  (based on code by Syahir Hakim)
  7.     License:     Share at will (with attribution)
  8.    
  9.     PLEASE DO NOT DELETE THIS INFO
  10. ********************************************************************************************/
  11.  
  12.  
  13. /********************************************************************************************
  14.     INSTRUCTIONS:
  15.    
  16.     A.  What It Does:
  17.     This script allows you to display custom header images for specific category archive
  18.     pages and single posts in the Graphene Theme based on your category hierarchy.
  19.     It supports up to 3 levels of depth.
  20.    
  21.     B.  How to Use It:
  22.         In order for the script to work, you need to:
  23.         1.  Create a folder called "custom-headers" inside the folder "graphene/images/";
  24.         2.  Using the category slugs as folder names, create a directory tree inside that
  25.             folder that mirrors the category hierarchy of the categories for which you
  26.             wish to display custom images;
  27.         3.  Upload images (.JPG only) to those folders; and
  28.         4.  Insert all LEVEL 1 (i.e. main) category IDs you wish to target as a comma-
  29.             separated list inside the $image_cats array below. There is no need to
  30.             include any LEVEL 2 or LEVEL 3 categories (i.e., sub-categories) in the array:
  31.             the script will retrieve custom images for all sub-categories under the targeted
  32.             LEVEL 1 categories.
  33.    
  34.     C.  How It Works:
  35.         The script checks if the current archive or post category is targeted in the array,
  36.         and if it is, it checks if a custom image folder exists for that cat and if contains
  37.         images. If it does, it wil fetch them and displays a random one. If it does not, it
  38.         will fetch the images from the LEVEL 1 category and display a random one.
  39.          
  40.     D.  An Example:
  41.         Let's suppose you have the following category structure. The cat ID of each
  42.         category is shown in parenthesis. A star symbol (*) next to a main category means
  43.         I am targeting that category (i.e., I have included it in the array).
  44.         An at symbol (@) next to any category means I would like to display custom images
  45.         for that category:
  46.        
  47.         People (2) @ *
  48.             - Family (12) @
  49.                 -- Close Family (41) @
  50.                 -- Extended Family (39)
  51.             - Friends (15)
  52.                 -- School (30) @
  53.                 -- Work (32)
  54.         Objects (5) @ *
  55.             - Tools (10) @
  56.                 -- Utensils (25) @
  57.                 -- Computers (27)
  58.         Places (7) *
  59.         Hobbies (8)
  60.                        
  61.         Based on that, my $image_cats array would be defined as such:
  62.         $image_cats = array(2,5,7);
  63.        
  64.         I would have to create the following directories inside
  65.         "grapehne/images" and upload images to each one:
  66.             /custom-headers/
  67.             /custom-headers/people/
  68.             /custom-headers/people/family/
  69.             /custom-headers/people/family/close-family/
  70.             /custom-headers/people/friends/
  71.             /custom-headers/people/friends/school/
  72.             /custom-headers/objects/
  73.             /custom-headers/objects/tools/
  74.             /custom-headers/objects/tools/utensils/
  75.        
  76.         Now, while navigating through the site, here's what I'll see inside
  77.         each category:
  78.           - Inside People | Images from /people/;
  79.           - Inside People -> Family | Images from /people/family/;
  80.           - Inside People -> Family -> Close Family | Images from
  81.              /people/family/close-family/;
  82.           - Inside People -> Family -> Extended Family | Images from /people/;
  83.           - Inside People -> Friends | Images from /people/;
  84.           - Inside People -> Friends -> School | Images from
  85.              /people/friends/school/;
  86.           - Inside People -> Friends -> Work | Images from /people/;   
  87.           - Inside Places (or its sub-cats) | Default images, because, even though
  88.             the category is included in the $image_cats array, there is no
  89.             folder /custom-headers/places/;
  90.           - Inside Hobbies (or its sub-cats) | Default images.
  91.          
  92. YOI CAN DELETE THESE INSTRUCTIONS OR SAVE THEM TO A TEXT FILE IF DESIRED
  93. ********************************************************************************************/
  94.  
  95.  
  96. /********************************************************************************************
  97.     SCRIPT START
  98. ********************************************************************************************/
  99.  
  100. function custom_header_image($random_image) {
  101.  
  102. /* CATEGORIES FOR CUSTOM IMAGES */   
  103.        $image_cats = array(4,6,8,9); // modify at will
  104.        
  105. /* GET CURRENT CAT AND INFO */
  106.     // Test if we are inside a cat archive or a post
  107.     if ( is_category() || is_single() ){
  108.         // Test if we are inside a cat archive
  109.         if (is_category() ) {
  110.             // If we are, get cat and cat info
  111.             $current_cat_id = get_query_var( 'cat' );
  112.             $current_cat_array = get_category($current_cat_id);
  113.             $current_cat_slug = $current_cat_array->slug;
  114.             $parent_cat_id = $current_cat_array->parent;
  115.         // If we are not, we are inside a single post
  116.         } else {
  117.             // Get cat and cat info
  118.             global $post;
  119.             $current_cat_array = get_the_category( $post->ID );
  120.             $current_cat_id = $current_cat_array[0]->cat_ID;
  121.             $current_cat_slug = $current_cat_array[0]->slug;
  122.             $parent_cat_id = $current_cat_array[0]->parent;
  123.         }
  124.         // Get the rest of the info
  125.         $parent_cat_array = get_category($parent_cat_id);
  126.         $parent_cat_slug = $parent_cat_array->slug;
  127.         $ancestor_cat_id = $parent_cat_array->parent;
  128.         $ancestor_cat_array = get_category($ancestor_cat_id);
  129.         $ancestor_cat_slug = $ancestor_cat_array->slug;
  130.            
  131. /* GET CUSTOM IMAGES FOR CATS */
  132.         // Check if we are dealing with one of the selected categories
  133.         if ( in_array( $current_cat_id, $image_cats ) || in_array( $parent_cat_id, $image_cats )|| in_array( $ancestor_cat_id, $image_cats ) ) {
  134.         // If we are, put together the absolute path
  135.             $current_cat_slug_slash = $current_cat_slug . '/';
  136.             $parent_cat_slug_slash = $parent_cat_slug . '/';
  137.             $ancestor_cat_slug_slash = $ancestor_cat_slug . '/';
  138.             $image_start_path = get_stylesheet_directory() . '/images/custom-headers/';
  139.             $image_start_dir = get_stylesheet_directory_uri() . '/images/custom-headers/';
  140.             $custom_images = '';
  141.             // Check which directory to retrieve the custom images from and save them in a variable
  142.             if ( $ancestor_cat_slug ) {
  143.                 $image_abs_path = $image_start_path . $ancestor_cat_slug_slash . $parent_cat_slug_slash . $current_cat_slug_slash;
  144.                 if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) {
  145.                     $image_dir = $image_start_dir . $ancestor_cat_slug_slash . $parent_cat_slug_slash . $current_cat_slug_slash;
  146.                 } else {
  147.                     $image_abs_path = $image_start_path . $ancestor_cat_slug_slash;
  148.                     $image_dir = $image_start_dir . $ancestor_cat_slug_slash;                  
  149.                     $custom_images = glob( $image_abs_path . "/*.jpg" );
  150.                 }
  151.             }
  152.             elseif ( !$ancestor_cat_slug && $parent_cat_slug ) {
  153.                 $image_abs_path = $image_start_path . $parent_cat_slug_slash . $current_cat_slug_slash;
  154.                 if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) {
  155.                     $image_dir = $image_start_dir . $parent_cat_slug_slash . $current_cat_slug_slash;
  156.                 } else {
  157.                     $image_abs_path = $image_start_path . $parent_cat_slug_slash;
  158.                     $image_dir = $image_start_dir . $parent_cat_slug_slash;
  159.                     $custom_images = glob( $image_abs_path . "/*.jpg" );
  160.                 }
  161.             }
  162.             elseif ( $current_cat_slug && !$parent_cat_slug ) {
  163.                 $image_abs_path = $image_start_path . $current_cat_slug_slash;
  164.                 if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) {
  165.                     $image_dir = $image_start_dir . $current_cat_slug_slash;
  166.                 }
  167.             }
  168.             // Retrieve random images from custom image  obtained
  169.             if ( $custom_images ) {
  170.                 $key = array_rand( $custom_images );
  171.                 $random_image = $image_dir . basename( $custom_images[$key] );
  172.             }
  173.         }
  174.     }
  175.     // Return the random image obtained
  176.     return $random_image;
  177. }
  178. add_filter( 'graphene_header_image', 'custom_header_image' );
  179.  
  180. /********************************************************************************************
  181.     SCRIPT END
  182. ********************************************************************************************/
  183.  
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement