Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 7th, 2012  |  syntax: None  |  size: 1.90 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php # -*- coding: utf-8 -*-
  2. /**
  3.  * Plugin Name: T5 Parent Terms in body_class
  4.  * Description: Adds all parent terms of a term archive to <code>body_class()</code>.
  5.  * Version:     2012.0.14
  6.  * Author:      Thomas Scholz <info@toscho.de>
  7.  * Author URI:  http://toscho.de
  8.  * License:     MIT
  9.  * License URI: http://www.opensource.org/licenses/mit-license.php
  10.  */
  11.  
  12. add_filter( 'body_class', 't5_parent_terms_to_body_class' );
  13.  
  14. function t5_parent_terms_to_body_class( $classes )
  15. {
  16.         if ( ! isset ( get_queried_object()->taxonomy ) && ! is_singular() )
  17.         {
  18.                 return $classes; // not a term archive and not a single page
  19.         }
  20.  
  21.         $current_terms = array ();
  22.         $new_classes   = array ();
  23.  
  24.         if ( is_singular() )
  25.         {
  26.                 $post_id    = get_the_ID();
  27.                 $post       = get_post( $post_id );
  28.                 $taxonomies = get_object_taxonomies( $post );
  29.                 $terms      = array ();
  30.  
  31.                 if ( empty ( $taxonomies ) )
  32.                 {
  33.                         return $classes;
  34.                 }
  35.  
  36.                 foreach ( $taxonomies as $taxonomy )
  37.                 {
  38.                         $tax_terms = get_the_terms( $post_id, $taxonomy );
  39.                         ! empty ( $tax_terms) and $terms = array_merge( $terms, $tax_terms );
  40.                 }
  41.  
  42.                 foreach ( $terms as $term )
  43.                 {
  44.                         if ( 0 !== $term->parent )
  45.                         {
  46.                                 $term_ancestors = get_ancestors(
  47.                                         $term->term_id,
  48.                                         $term->taxonomy
  49.                                 );
  50.  
  51.                                 foreach ( $term_ancestors as $term_ancestor )
  52.                                 {
  53.                                         $term_data      = get_term( $term_ancestor, $term->taxonomy );
  54.                                         $new_classes[] = esc_attr( "$term->taxonomy-$term_data->slug" );
  55.                                 }
  56.                         }
  57.                 }
  58.         }
  59.         else
  60.         {
  61.                 // get all ancestors as an array.
  62.                 $ancestors = get_ancestors(
  63.                         get_queried_object_id(),
  64.                         get_queried_object()->taxonomy
  65.                 );
  66.  
  67.                 if ( empty ( $ancestors ) )
  68.                 {
  69.                         return $classes;
  70.                 }
  71.  
  72.                 foreach ( $ancestors as $ancestor )
  73.                 {
  74.                         $term          = get_term( $ancestor, get_queried_object()->taxonomy );
  75.                         $new_classes[] = esc_attr( "$term->taxonomy-$term->slug" );
  76.                 }
  77.         }
  78.  
  79.         // combine both
  80.         $classes_out = array_merge( $classes, $new_classes );
  81.  
  82.         return array_unique( $classes_out );
  83. }