View difference between Paste ID: c20yEzMW and LiEfKdzh
SHOW: | | - or go back to the newest paste.
1
<?php
2
/*
3
 * Tests if any of a post's assigned categories are descendants of target categories
4
 *
5
 * @param int|array $cats The target categories. Integer ID or array of integer IDs
6
 * @param int|object $_post The post. Omit to test the current post in the Loop or main query
7
 * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
8
 * @see get_term_by() You can get a category by name or slug, then pass ID to this function
9
 * @uses get_term_children() Passes $cats
10
 * @uses in_category() Passes $_post (can be empty)
11
 * @version 2.7
12
 * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
13
 */
14
 
15
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
16
	function post_is_in_descendant_category( $cats, $_post = null ) {
17
		foreach ( (array) $cats as $cat ) {
18
			// get_term_children() accepts integer ID only
19
			$descendants = get_term_children( (int) $cat, 'category' );
20
			if ( $descendants && in_category( $descendants, $_post ) )
21
				return true;
22
		}
23
		return false;
24
	}
25
}
26
27-
function graphene_custom_header_image( $image ){
27+
function graphene_custom_header_image( $header_img ){
28-
	if ( in_category(52) || post_is_in_descendant_category(52) ){
28+
	if ( in_category(4) || post_is_in_descendant_category(4) ){
29
		/* The script to rotate through possible images here. */
30
31
		// Get the current category
32
		if ( $cat = get_query_var( 'cat' ) ){
33
			$cat = get_the_terms( $cat, 'category' );
34
			$cat = $cat[1]->slug;
35
		} else {
36
			global $post;
37
			$cat = get_the_category( $post->ID );
38
			$cat = $cat[0]->slug;
39
		}
40
41
		// The directory where the images reside
42
		$dir = get_stylesheet_directory() . '/images/custom-headers/' . $cat;
43
44
	  	if ( is_dir( $dir ) ){
45
			// Get all images from the $dir above
46
			$images = glob( $dir . "/*.jpg" );
47
48
			if ( $images ){
49
			   // Pick a random image
50
			   $key = array_rand( $images );
51
52
			   // Make sure the resulting image is assigned to the $image variable
53-
			   $image = get_stylesheet_directory_uri() . "/images/custom-headers/$cat/" . basename( $images[$key] );
53+
			   $header_img = get_stylesheet_directory_uri() . "/images/custom-headers/$cat/" . basename( $images[$key] );
54
			}
55
		}
56
	}
57-
   return $image;
57+
   return $header_img;
58
}
59
add_filter( 'graphene_header_image', 'graphene_custom_header_image' );
60
61
?>
62