Title: get_terms in functions.php
Last modified: August 30, 2016

---

# get_terms in functions.php

 *  Resolved [mmacfadden](https://wordpress.org/support/users/mmacfadden/)
 * (@mmacfadden)
 * [10 years, 5 months ago](https://wordpress.org/support/topic/get_terms-in-functionsphp/)
 * 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' );
       ```
   
 * [https://wordpress.org/plugins/custom-post-type-ui/](https://wordpress.org/plugins/custom-post-type-ui/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [10 years, 5 months ago](https://wordpress.org/support/topic/get_terms-in-functionsphp/#post-6868329)
 * As far as I can tell, that should work. The only real issue I can see would be
   no results coming back from the get_terms() call or perhaps the logic in the 
   first if statement not matching up like expected.
 *  Thread Starter [mmacfadden](https://wordpress.org/support/users/mmacfadden/)
 * (@mmacfadden)
 * [10 years, 5 months ago](https://wordpress.org/support/topic/get_terms-in-functionsphp/#post-6868369)
 * Michael, thanks so much for your reply. I figured out the problem, and it seems
   that:
    `add_action( 'widgets_init', 'course_sidebars' );` was getting called 
   before the plugin could do its magic. I changed the action reference to: `add_action('
   init', 'course_sidebars' );` Now everything seems to work as it should.
 * Thanks for building an awesome plugin!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘get_terms in functions.php’ is closed to new replies.

 * ![](https://ps.w.org/custom-post-type-ui/assets/icon-256x256.png?rev=2744389)
 * [Custom Post Type UI](https://wordpress.org/plugins/custom-post-type-ui/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-post-type-ui/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-post-type-ui/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-post-type-ui/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-post-type-ui/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-post-type-ui/reviews/)

## Tags

 * [get_terms](https://wordpress.org/support/topic-tag/get_terms/)

 * 2 replies
 * 2 participants
 * Last reply from: [mmacfadden](https://wordpress.org/support/users/mmacfadden/)
 * Last activity: [10 years, 5 months ago](https://wordpress.org/support/topic/get_terms-in-functionsphp/#post-6868369)
 * Status: resolved