Guest User

Hierarchy-based random header images for the Graphene theme

a guest
Nov 17th, 2011
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**********************************************************************************************
  2.     SCRIPT INFO:
  3.    
  4.     Description: Hierarchy-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, single posts, and pages in the Graphene Theme based on your category and/or
  19.     page hierarchy. 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 and page slugs as folder names, create a directory tree inside
  25.             that folder that mirrors the category and/or page hierarchy of the categories and
  26.             pages for which you 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 and page IDs you wish to target as a comma-
  29.             separated list inside the $custom_image_ids array below. There is no need to
  30.             include any LEVEL 2 or LEVEL 3 categories or pages (i.e., sub-categories or subpages)
  31.             in the array: the script will retrieve custom images for all sub-categories and
  32.             sub-pages under the targeted LEVEL 1 categories and pages.
  33.    
  34.     C.  How It Works:
  35.         The script checks if the current category archive, post category, or page is targeted
  36.         in the array, and if it is, it checks if a custom image folder exists for said cats or
  37.         pages and if they contain any images. If they do, it wil fetch them and display a random
  38.         one. If they do not, it will fetch the images from the LEVEL 1 category or page and
  39.         display a random one.
  40.          
  41.     D.  An Example:
  42.         Let's suppose you have the following category structure. The cat ID of each
  43.         category is shown in parenthesis. A star symbol (*) next to a main category means
  44.         I am targeting that category (i.e., I have included it in the array).
  45.         An at symbol (@) next to any category means I would like to display custom images
  46.         for that category:
  47.        
  48.         People (2) @ *
  49.             - Family (12) @
  50.                 -- Close Family (41) @
  51.                 -- Extended Family (39)
  52.             - Friends (15)
  53.                 -- School (30) @
  54.                 -- Work (32)
  55.         Objects (5) @ *
  56.             - Tools (10) @
  57.                 -- Utensils (25) @
  58.                 -- Computers (27)
  59.         Places (7) *
  60.         Hobbies (8)
  61.                        
  62.         Based on that, my $custom_image_ids array would be defined as such:
  63.         $custom_image_ids = array(2,5,7);
  64.        
  65.         I would have to create the following directories inside
  66.         "grapehne/images" and upload images to each one:
  67.             /custom-headers/
  68.             /custom-headers/people/
  69.             /custom-headers/people/family/
  70.             /custom-headers/people/family/close-family/
  71.             /custom-headers/people/friends/
  72.             /custom-headers/people/friends/school/
  73.             /custom-headers/objects/
  74.             /custom-headers/objects/tools/
  75.             /custom-headers/objects/tools/utensils/
  76.        
  77.         Now, while navigating through the site, here's what I'll see inside
  78.         each category:
  79.           - Inside People | Images from /people/;
  80.           - Inside People -> Family | Images from /people/family/;
  81.           - Inside People -> Family -> Close Family | Images from
  82.              /people/family/close-family/;
  83.           - Inside People -> Family -> Extended Family | Images from /people/;
  84.           - Inside People -> Friends | Images from /people/;
  85.           - Inside People -> Friends -> School | Images from
  86.              /people/friends/school/;
  87.           - Inside People -> Friends -> Work | Images from /people/;   
  88.           - Inside Places (or its sub-cats) | Default images, because, even though
  89.             the category is included in the $custom_image_ids array, there is no
  90.             folder /custom-headers/places/;
  91.           - Inside Hobbies (or its sub-cats) | Default images.
  92.          
  93.         The same logic would apply if we wanted to use pages for this, and the
  94.         directory tree would be composed of folders named after the page slugs.
  95.  
  96.          
  97. YOI CAN DELETE THESE INSTRUCTIONS OR SAVE THEM TO A TEXT FILE IF DESIRED
  98. ***********************************************************************************************/
  99.  
  100.  
  101. /***********************************************************************************************
  102.     SCRIPT START
  103. ***********************************************************************************************/
  104.  
  105. function custom_header_image($random_image) {
  106.  
  107. /* CATEGORY AND PAGE IDS FOR CUSTOM IMAGES */    
  108.        $custom_image_ids = array(4,6,8,9); // modify at will
  109.        
  110. /* GET CURRENT CAT OR PAGE INFO */
  111.     // Test if we are inside a cat archive, a post, or a page
  112.     if ( is_category() || is_single() || is_page() ){
  113.         // If we are, test if we are inside a cat archive or a post
  114.         if ( is_category() || is_single() ) {
  115.         // If we are, test if we are inside a cat archive
  116.             if (is_category() ) {
  117.                 // If we are, get cat and cat info
  118.                 $current_id = get_query_var( 'cat' );
  119.                 $current_array = get_category($current_id);
  120.                 $current_slug = $current_array->slug;
  121.                 $parent_id = $current_array->parent;
  122.             // If we are not, we are inside a single post
  123.             } else {
  124.                 // Get cat and cat info
  125.                 global $post;
  126.                 $current_array = get_the_category( $post->ID );
  127.                 $current_id = $current_array[0]->cat_ID;
  128.                 $current_slug = $current_array[0]->slug;
  129.                 $parent_id = $current_array[0]->parent;
  130.             }
  131.             // Get the rest of the cat info
  132.             $parent_array = get_category($parent_id);
  133.             $parent_slug = $parent_array->slug;
  134.             $ancestor_id = $parent_array->parent;
  135.             $ancestor_array = get_category($ancestor_id);
  136.             $ancestor_slug = $ancestor_array->slug;
  137.         // If it's neither a cat archive or a post, it's a page
  138.         } else {
  139.             // Get page and page info
  140.             global $post;
  141.             $current_array = get_page( $post->ID );
  142.             $current_id = $post->ID;
  143.             $current_slug = $post->post_name;
  144.             $current_ancestors = get_ancestors ($current_id, 'page');
  145.             $parent_id = $current_ancestors[0];
  146.             // Test if current page has a parent
  147.             if ( $parent_id ) {
  148.             $parent_array = get_page( $parent_id );
  149.             $parent_slug = $parent_array->post_name;
  150.             }
  151.             $ancestor_id = $current_ancestors[1];
  152.             // Test if current page has an ancestor
  153.             if ( $ancestor_id ) {
  154.             $ancestor_array = get_page( $ancestor_id );
  155.             $ancestor_slug = $ancestor_array->post_name;
  156.             }
  157.         }
  158.  
  159.  
  160. /* GET CUSTOM IMAGES FOR CATS */
  161.         // Check if we are dealing with one of the selected categories
  162.         if ( in_array( $current_id, $custom_image_ids ) || in_array( $parent_id, $custom_image_ids )|| in_array( $ancestor_id, $custom_image_ids ) ) {
  163.         // If we are, put together the absolute path
  164.             $current_slug_slash = $current_slug . '/';
  165.             $parent_slug_slash = $parent_slug . '/';
  166.             $ancestor_slug_slash = $ancestor_slug . '/';
  167.             $image_start_path = get_stylesheet_directory() . '/images/custom-headers/';
  168.             $image_start_dir = get_stylesheet_directory_uri() . '/images/custom-headers/';
  169.             $custom_images = '';
  170.             // Check which directory to retrieve the custom images from and save them in a variable
  171.             if ( $ancestor_slug ) {
  172.                 $image_abs_path = $image_start_path . $ancestor_slug_slash . $parent_slug_slash . $current_slug_slash;
  173.                 if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) {
  174.                     $image_dir = $image_start_dir . $ancestor_slug_slash . $parent_slug_slash . $current_slug_slash;
  175.                 } else {
  176.                     $image_abs_path = $image_start_path . $ancestor_slug_slash;
  177.                     $image_dir = $image_start_dir . $ancestor_slug_slash;                  
  178.                     $custom_images = glob( $image_abs_path . "/*.jpg" );
  179.                 }
  180.             }
  181.             elseif ( !$ancestor_slug && $parent_slug ) {
  182.                 $image_abs_path = $image_start_path . $parent_slug_slash . $current_slug_slash;
  183.                 if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) {
  184.                     $image_dir = $image_start_dir . $parent_slug_slash . $current_slug_slash;
  185.                 } else {
  186.                     $image_abs_path = $image_start_path . $parent_slug_slash;
  187.                     $image_dir = $image_start_dir . $parent_slug_slash;
  188.                     $custom_images = glob( $image_abs_path . "/*.jpg" );
  189.                 }
  190.             }
  191.             elseif ( $current_slug && !$parent_slug ) {
  192.                 $image_abs_path = $image_start_path . $current_slug_slash;
  193.                 if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) {
  194.                     $image_dir = $image_start_dir . $current_slug_slash;
  195.                 }
  196.             }
  197.             // Retrieve random images from custom image  obtained
  198.             if ( $custom_images ) {
  199.                 $key = array_rand( $custom_images );
  200.                 $random_image = $image_dir . basename( $custom_images[$key] );
  201.             }
  202.         }
  203.     }
  204.     // Return the random image obtained
  205.     return $random_image;
  206. }
  207. add_filter( 'graphene_header_image', 'custom_header_image' );
  208.  
  209. /***********************************************************************************************
  210.     SCRIPT END
  211. ***********************************************************************************************/
  212.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×