Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Tests if any of a post's assigned categories are descendants of target categories
- *
- * @param int|array $cats The target categories. Integer ID or array of integer IDs
- * @param int|object $_post The post. Omit to test the current post in the Loop or main query
- * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
- * @see get_term_by() You can get a category by name or slug, then pass ID to this function
- * @uses get_term_children() Passes $cats
- * @uses in_category() Passes $_post (can be empty)
- * @version 2.7
- * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
- */
- if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
- function post_is_in_descendant_category( $cats, $_post = null ) {
- foreach ( (array) $cats as $cat ) {
- // get_term_children() accepts integer ID only
- $descendants = get_term_children( (int) $cat, 'category' );
- if ( $descendants && in_category( $descendants, $_post ) )
- return true;
- }
- return false;
- }
- }
- function graphene_custom_header_image( $header_img ){
- if ( in_category(4) || post_is_in_descendant_category(4) ){
- /* The script to rotate through possible images here. */
- // Get the current category
- if ( $cat = get_query_var( 'cat' ) ){
- $cat = get_the_terms( $cat, 'category' );
- $cat = $cat[1]->slug;
- } else {
- global $post;
- $cat = get_the_category( $post->ID );
- $cat = $cat[0]->slug;
- }
- // The directory where the images reside
- $dir = get_stylesheet_directory() . '/images/custom-headers/' . $cat;
- if ( is_dir( $dir ) ){
- // Get all images from the $dir above
- $images = glob( $dir . "/*.jpg" );
- if ( $images ){
- // Pick a random image
- $key = array_rand( $images );
- // Make sure the resulting image is assigned to the $image variable
- $header_img = get_stylesheet_directory_uri() . "/images/custom-headers/$cat/" . basename( $images[$key] );
- }
- }
- }
- return $header_img;
- }
- add_filter( 'graphene_header_image', 'graphene_custom_header_image' );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement