Advertisement
Guest User

dynamic category sidebar

a guest
Nov 27th, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.90 KB | None | 0 0
  1. function abc_show_sidebar() {
  2.     // default value
  3.     $sidebar_id = 'sidebar';
  4.    
  5.     // first check to see if this is a category page
  6.     if ( is_cat() ) {
  7.         $sidebar_id = sanitize_title( get_cat_name( get_query_var( 'cat' ) ) ) . '-sidebar';
  8.     }
  9.     // now check if this is a single post page
  10.     elseif ( is_singular( 'post' ) ) {
  11.         // grab the $post object
  12.         $post = get_post();
  13.         // here is where you have to make the assumption that the post
  14.         // has only one category assigned to it
  15.         // get_the_category() returns an array of cat objects assigned to the post
  16.         $categories = get_the_category( $post->ID );
  17.         // use only the first
  18.         $category = $categories[0];
  19.         // this will go up through the category tree to find the top-level parent
  20.         while ( 0 != $category->parent ) {
  21.             $category = get_category( $category->parent );
  22.         }
  23.         $sidebar_id = $category->cat_name;
  24.     }
  25.    
  26.     dynamic_sidebar( $sidebar_id );
  27.    
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement