get_terms in functions.php
-
I’m trying to dynamically create new widget areas each time I create a new instance of a custom taxonomy for a custom post type. The post type I created is called “lesson” and the custom taxonomy is called “course.”
I’m able to do something similar with “posts” and “categories” using the following code:
/* Create widgetized sidebars for each category */ function category_sidebars() { $categories = get_categories( array( 'hide_empty'=> 0 ) ); foreach ( $categories as $category ) { if ( 0 == $category->parent ) register_sidebar( array( 'name' => $category->cat_name, 'id' => $category->category_nicename . '-sidebar', 'description' => 'This is the ' . $category->cat_name . ' widgetized area', 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', ) ); } } add_action( 'widgets_init', 'category_sidebars' );
The code I’m using for my custom post type and taxonomy is below. I’ve placed it in my functions.php file, but I can’t seem to get $term->name or $term->slug to show up. Any thoughts? Thanks!
/* Create widgetized sidebars for each course*/ function course_sidebars() { $terms = get_terms( 'course' ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ foreach ( $terms as $term ) { if ( 0 == $term->parent ) { register_sidebar( array( 'name' => $term->slug, 'id' => $term->name . '-sidebar', 'description' => 'This is the ' . $term->slug . ' widgetized area', 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', ) ); } } } } add_action( 'widgets_init', 'course_sidebars' );
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘get_terms in functions.php’ is closed to new replies.