Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function abc_show_sidebar() {
- // default value
- $sidebar_id = 'sidebar';
- // first check to see if this is a category page
- if ( is_cat() ) {
- $sidebar_id = sanitize_title( get_cat_name( get_query_var( 'cat' ) ) ) . '-sidebar';
- }
- // now check if this is a single post page
- elseif ( is_singular( 'post' ) ) {
- // grab the $post object
- $post = get_post();
- // here is where you have to make the assumption that the post
- // has only one category assigned to it
- // get_the_category() returns an array of cat objects assigned to the post
- $categories = get_the_category( $post->ID );
- // use only the first
- $category = $categories[0];
- // this will go up through the category tree to find the top-level parent
- while ( 0 != $category->parent ) {
- $category = get_category( $category->parent );
- }
- $sidebar_id = $category->cat_name;
- }
- dynamic_sidebar( $sidebar_id );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement