Advertisement
Andrea155

Untitled

Oct 14th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. function custom_header_image($random_image) {
  2.  
  3. /* CATEGORY AND PAGE IDS FOR CUSTOM IMAGES */
  4. $custom_image_ids = array(2,8,28,617); // modify at will
  5.  
  6. /* GET CURRENT CAT OR PAGE INFO */
  7. // Test if we are inside a cat archive, a post, or a page
  8. if ( is_category() || is_single() || is_page() ){
  9.  
  10. /* CHECK IF POST HAS FEATURED IMAGE */
  11. if (is_single() || is_page() ) {
  12. global $post;
  13. $post_id = $post->ID;
  14. $featured_array = wp_get_attachment_image_src( get_post_thumbnail_id($post_id), 'full');
  15. $featured_image = $featured_array[0];
  16.  
  17. // Check if this is a post
  18. if ( is_single() ) {
  19. // Retrieve post info
  20. $curr_post_id = $post_id;
  21. $current_args = array('orderby' => 'ID', 'order' => 'DESC');
  22. $current_array = wp_get_post_terms( $curr_post_id, 'category', $current_args );
  23. $current_id = $current_array[0]->cat_ID;
  24. $current_slug = $current_array[0]->slug;
  25. $parent_id = $current_array[0]->parent;
  26. // Test if current post cat has parent cat
  27. if ( $parent_id ) {
  28. $parent_array = get_term($parent_id,'category');
  29. $parent_slug = $parent_array->slug;
  30. $ancestor_id = $parent_array->parent;
  31. // Test if current post cat has ancestor cat
  32. if ( $ancestor_id ) {
  33. $ancestor_array = get_term($ancestor_id,'category');
  34. $ancestor_slug = $ancestor_array->slug;
  35. }
  36. }
  37. // If it is not a post, it's a page
  38. } else {
  39. // Retrieve page info
  40. $curr_post_id = $post->ID;
  41. $current_array = get_page( $curr_post_id );
  42. $current_id = $curr_post_id;
  43. $current_slug = $post->post_name;
  44. $current_ancestors = get_ancestors ($current_id, 'page');
  45. $parent_id = $current_ancestors[0];
  46. // Test if current page has a parent
  47. if ( $parent_id ) {
  48. $parent_array = get_page( $parent_id );
  49. $parent_slug = $parent_array->post_name;
  50. $ancestor_id = $current_ancestors[1];
  51. // Test if current page has an ancestor
  52. if ( $ancestor_id ) {
  53. $ancestor_array = get_page( $ancestor_id );
  54. $ancestor_slug = $ancestor_array->post_name;
  55. }
  56. }
  57. }
  58. // If it's neither a post nor a page, it's a category
  59. } else {
  60. // Define unneeded $post_id value as false
  61. $post_id = false;
  62. // Retrieve cat info
  63. $current_id = get_query_var( 'cat' );
  64. $current_array = get_term($current_id,'category');
  65. $current_slug = $current_array->slug;
  66. $parent_id = $current_array->parent;
  67. if ( $parent_id ) {
  68. $parent_array = get_term($parent_id,'category');
  69. $parent_slug = $parent_array->slug;
  70. $ancestor_id = $parent_array->parent;
  71. if ( $ancestor_id ) {
  72. $ancestor_array = get_term($ancestor_id,'category');
  73. $ancestor_slug = $ancestor_array->slug;
  74. }
  75. }
  76. }
  77.  
  78. if ( $featured_image ) {
  79. $random_image = $featured_image;
  80. } else {
  81.  
  82. /* GET CUSTOM IMAGES FOR CATS */
  83. // Check if we are dealing with one of the selected categories
  84. if ( in_array( $current_id, $custom_image_ids ) || in_array( $parent_id, $custom_image_ids )|| in_array( $ancestor_id, $custom_image_ids ) ) {
  85. // If we are, put together the absolute path
  86. $current_slug_slash = $current_slug . '/';
  87. $parent_slug_slash = $parent_slug . '/';
  88. $ancestor_slug_slash = $ancestor_slug . '/';
  89. $image_start_path = get_stylesheet_directory() . '/images/custom-headers/';
  90. $image_start_dir = get_stylesheet_directory_uri() . '/images/custom-headers/';
  91. $custom_images = '';
  92. // Check which directory to retrieve the custom images from and save them in a variable
  93. if ( $ancestor_slug ) {
  94. $image_abs_path = $image_start_path . $ancestor_slug_slash . $parent_slug_slash . $current_slug_slash;
  95. if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) {
  96. $image_dir = $image_start_dir . $ancestor_slug_slash . $parent_slug_slash . $current_slug_slash;
  97. } else {
  98. $image_abs_path = $image_start_path . $ancestor_slug_slash;
  99. $image_dir = $image_start_dir . $ancestor_slug_slash;
  100. $custom_images = glob( $image_abs_path . "/*.jpg" );
  101. }
  102. }
  103. elseif ( !$ancestor_slug && $parent_slug ) {
  104. $image_abs_path = $image_start_path . $parent_slug_slash . $current_slug_slash;
  105. if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) {
  106.  
  107. $image_dir = $image_start_dir . $parent_slug_slash . $current_slug_slash;
  108. } else {
  109. $image_abs_path = $image_start_path . $parent_slug_slash;
  110. $image_dir = $image_start_dir . $parent_slug_slash;
  111. $custom_images = glob( $image_abs_path . "/*.jpg" );
  112. }
  113. }
  114. elseif ( $current_slug && !$parent_slug ) {
  115. $image_abs_path = $image_start_path . $current_slug_slash;
  116. if ( is_dir( $image_abs_path ) && $custom_images = glob( $image_abs_path . "/*.jpg" ) ) {
  117. $image_dir = $image_start_dir . $current_slug_slash;
  118. }
  119. }
  120. // Retrieve random images from custom image obtained
  121. if ( $custom_images ) {
  122. $key = array_rand( $custom_images );
  123. $random_image = $image_dir . basename( $custom_images[$key] );
  124. }
  125. }
  126. }
  127. }
  128. // Return the random image obtained
  129. return $random_image;
  130. }
  131. add_filter( 'graphene_header_image', 'custom_header_image' );
  132.  
  133. /***********************************************************************************************
  134. SCRIPT END
  135. ***********************************************************************************************/
  136. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement