Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function custom_header_image($random_image) {
- /* CATEGORY AND PAGE IDS FOR CUSTOM IMAGES */
- $custom_image_ids = array(2,8,28,617); // modify at will
- /* GET CURRENT CAT OR PAGE INFO */
- // Test if we are inside a cat archive, a post, or a page
- if ( is_category() || is_single() || is_page() ){
- /* CHECK IF POST HAS FEATURED IMAGE */
- if (is_single() || is_page() ) {
- global $post;
- $post_id = $post->ID;
- $featured_array = wp_get_attachment_image_src( get_post_thumbnail_id($post_id), 'full');
- $featured_image = $featured_array[0];
- // Check if this is a post
- if ( is_single() ) {
- // Retrieve post info
- $curr_post_id = $post_id;
- $current_args = array('orderby' => 'ID', 'order' => 'DESC');
- $current_array = wp_get_post_terms( $curr_post_id, 'category', $current_args );
- $current_id = $current_array[0]->cat_ID;
- $current_slug = $current_array[0]->slug;
- $parent_id = $current_array[0]->parent;
- // Test if current post cat has parent cat
- if ( $parent_id ) {
- $parent_array = get_term($parent_id,'category');
- $parent_slug = $parent_array->slug;
- $ancestor_id = $parent_array->parent;
- // Test if current post cat has ancestor cat
- if ( $ancestor_id ) {
- $ancestor_array = get_term($ancestor_id,'category');
- $ancestor_slug = $ancestor_array->slug;
- }
- }
- // If it is not a post, it's a page
- } else {
- // Retrieve page info
- $curr_post_id = $post->ID;
- $current_array = get_page( $curr_post_id );
- $current_id = $curr_post_id;
- $current_slug = $post->post_name;
- $current_ancestors = get_ancestors ($current_id, 'page');
- $parent_id = $current_ancestors[0];
- // Test if current page has a parent
- if ( $parent_id ) {
- $parent_array = get_page( $parent_id );
- $parent_slug = $parent_array->post_name;
- $ancestor_id = $current_ancestors[1];
- // Test if current page has an ancestor
- if ( $ancestor_id ) {
- $ancestor_array = get_page( $ancestor_id );
- $ancestor_slug = $ancestor_array->post_name;
- }
- }
- }
- // If it's neither a post nor a page, it's a category
- } else {
- // Define unneeded $post_id value as false
- $post_id = false;
- // Retrieve cat info
- $current_id = get_query_var( 'cat' );
- $current_array = get_term($current_id,'category');
- $current_slug = $current_array->slug;
- $parent_id = $current_array->parent;
- if ( $parent_id ) {
- $parent_array = get_term($parent_id,'category');
- $parent_slug = $parent_array->slug;
- $ancestor_id = $parent_array->parent;
- if ( $ancestor_id ) {
- $ancestor_array = get_term($ancestor_id,'category');
- $ancestor_slug = $ancestor_array->slug;
- }
- }
- }
- if ( $featured_image ) {
- $random_image = $featured_image;
- } else {
- /* GET CUSTOM IMAGES FOR CATS */
- // Check if we are dealing with one of the selected categories
- if ( in_array( $current_id, $custom_image_ids ) || in_array( $parent_id, $custom_image_ids )|| in_array( $ancestor_id, $custom_image_ids ) ) {
- // If we are, put together the absolute path
- $current_slug_slash = $current_slug . '/';
- $parent_slug_slash = $parent_slug . '/';
- $ancestor_slug_slash = $ancestor_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_slug ) {
- $image_abs_path = $image_start_path . $ancestor_slug_slash . $parent_slug_slash . $current_slug_slash;
- if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) {
- $image_dir = $image_start_dir . $ancestor_slug_slash . $parent_slug_slash . $current_slug_slash;
- } else {
- $image_abs_path = $image_start_path . $ancestor_slug_slash;
- $image_dir = $image_start_dir . $ancestor_slug_slash;
- $custom_images = glob( $image_abs_path . "/*.jpg" );
- }
- }
- elseif ( !$ancestor_slug && $parent_slug ) {
- $image_abs_path = $image_start_path . $parent_slug_slash . $current_slug_slash;
- if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) {
- $image_dir = $image_start_dir . $parent_slug_slash . $current_slug_slash;
- } else {
- $image_abs_path = $image_start_path . $parent_slug_slash;
- $image_dir = $image_start_dir . $parent_slug_slash;
- $custom_images = glob( $image_abs_path . "/*.jpg" );
- }
- }
- elseif ( $current_slug && !$parent_slug ) {
- $image_abs_path = $image_start_path . $current_slug_slash;
- if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) {
- $image_dir = $image_start_dir . $current_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
- ***********************************************************************************************/
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement