Instantly share code, notes, and snippets.
Created
September 2, 2013 14:23
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save deadlyhifi/6413397 to your computer and use it in GitHub Desktop.
Display parent and child level taxonomy (category) dependent on what content is currently being displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function pi_conditions_overview() { | |
$conditions = new Tax_List( 'condition' ); // put in name of taxonomy | |
$conditions->display_html(); | |
} | |
class Tax_List { | |
private $taxonomy; | |
private $post_id; | |
public function __construct( $taxonomy ) { | |
$this->taxonomy = $taxonomy; | |
$this->populate_posts_id(); | |
} | |
// if this is a single post get the ID. | |
private function populate_posts_id() { | |
if ( ! is_single() ) return; | |
$this->post_id = get_the_ID(); | |
} | |
// get just the parent categories. | |
private function get_parent_terms() { | |
// all terms | |
$args = array( | |
// 'orderby' => 'name', | |
// 'order' => 'ASC', | |
'hide_empty' => false, | |
// 'exclude' => array(), | |
// 'exclude_tree' => array(), | |
// 'include' => array(), | |
// 'number' => '', | |
// 'fields' => 'all', | |
// 'slug' => '', | |
'parent' => 0, | |
// 'hierarchical' => true, | |
// 'child_of' => 0, | |
// 'get' => '', | |
// 'name__like' => '', | |
// 'pad_counts' => false, | |
// 'offset' => '', | |
// 'search' => '', | |
// 'cache_domain' => 'core' | |
); | |
return get_terms( $this->taxonomy, $args ); | |
} | |
/* =========================================== | |
//! Taxonomy checks on current content | |
============================================*/ | |
// are we on a category archive page? | |
private function is_this_a_taxonomy( $term_id ) { | |
$is_tax = is_tax( $this->taxonomy, $term_id ); | |
return $is_tax; | |
} | |
// are we on a child category archive page? | |
private function is_this_a_child_taxonomy( $term_id ) { | |
return get_queried_object()->parent === $term_id; | |
} | |
// is_single check if it’s terms correspond in the taxonomy list. | |
// return array of associated term id’s | |
private function is_this_a_post( $term_id ) { | |
if ( ! $this->post_id ) return; | |
$terms = wp_get_post_terms( $this->post_id, $this->taxonomy ); | |
//echo '<pre>' . $this->post_id . '<br/>'.print_r($terms,1).'</pre>'; | |
if ( ! $terms ) return; | |
// each term check if it’s the parent or child of. | |
foreach ( $terms as $term ) | |
if ( $term_id === $term->term_id || $term_id === $term->parent ) | |
$r[] = $term->term_id; | |
return ( $r ? $r : false ); | |
} | |
// determine if this post/cpt post/taxonomy is current, and therefore needs highlighting. | |
private function needs_highlight( $term_id, $term_list ) { | |
if ( $this->is_this_a_taxonomy( $term_id ) ) | |
return true; | |
elseif ( is_array( $term_list ) && in_array( $term_id, $term_list ) ) | |
return true; | |
} | |
/* =========================================== | |
//! Views | |
============================================*/ | |
// our output html | |
public function display_html() { | |
$r = '<ul>'; | |
foreach ( $this->get_parent_terms() as $term ) { | |
$is_parent = $this->is_this_a_taxonomy( $term->term_id ); // is this the parent? | |
$is_child = $this->is_this_a_child_taxonomy( $term->term_id ); // is this a child of the term? | |
$is_single = $this->is_this_a_post( $term->term_id ); // does this post contain the parent or children of the term? | |
// if yes to either we get the child items. | |
$children = ( $is_parent || $is_child || $is_single ? get_term_children( $term->term_id, $this->taxonomy ) : null ); | |
$r .= '<li' . ( $this->needs_highlight( $term->term_id, $is_single ) ? ' class="current"' : null ) . '><a href="' . get_term_link( $term->name, 'condition' ) . '">' . $term->name . '</a>'; | |
// if this taxonomy has children | |
if ( $children ) : | |
$r .= '<ul>'; | |
foreach ( $children as $child ) { | |
$childterm = get_term_by( 'id', $child, $this->taxonomy ); | |
$r .= '<li' . ( $this->needs_highlight( $childterm->term_id, $is_single ) ? ' class="current"' : null ) . '><a href="' . get_term_link( $childterm->name, $this->taxonomy ) . '">' . $childterm->name . '</a></li>'; | |
} | |
$r .= '</ul>'; | |
endif; | |
$r .= '</li>'; | |
} | |
$r .= '</ul>'; | |
echo $r; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment