• I have a custom WordPress theme. I have a custom taxonomy – taluka added to the regular posts with default category. There are different posts with taluka taxonomy and default categories. I want to display the categories to the respective taxonomy

    
    <ul>
    	<li>-Taluka 1
             <ul>
                <li>- Category1 </li>
                <li>- Category2</li>
    	   <li>- Category3</li>
             <ul>	
          </li>
         <li>-Taluka 2
             <ul>
                <li>- Category2 </li>
                <li>- Category3</li>
    	   <li>- Category4</li>
             <ul>	
          </li>
         <li>-Taluka 3
             <ul>
                <li>- Category1 </li>
                <li>- Category4</li>
    	   <li>- Category5</li>
             <ul>	
          </li>
    </ul>
    

    I tried the following code but it display all the categories

    
    
     
    			  $terms = get_terms('taluka', array('hide_empty'=> true, 'orderby' => 'title', 'order'=> 'ASC',)); 
    
    			  $categories =  get_categories($terms); 
    
    			  $cat_slug = $category->slug;
    
    			 echo '<ul class="test">';
    
    			   foreach ( $terms as $term ) {
    
                        $termname = strtolower($term->name);  
    
                        $termname = str_replace(' ', '-', $termname);  
    
                     echo '<li>'. $term->name ;
    	 if ( !empty( $categories ) && !is_wp_error( $categories ) ){
    
    	 echo '<ul class="subtest">';
    
         foreach ( $categories as $category ) {;
    
           echo '<li data-filter="'.$category->slug.'">'.$category->name.'</li>';
        		}
    		echo '</ul>';
    			 }
    	 echo '</li>';
    		   };
    	echo '</ul>';
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The return of get_terms() is not a valid argument for get_categories(). Thus your arguments are ignored and the function returns all category terms.

    First let me define some terminology to help ensure we are talking about the same things. A taxonomy is an abstract grouping mechanism for certain post_types. A taxonomy has several terms, each of which is a trait shared by a particular group of posts. A taxonomy is related to one or more post_types. The taxonomy’s terms are assigned to individual posts of the related post_type.

    The default category taxonomy is confusing because we also refer to its terms as categories. A better example would be a “food_group” taxonomy that has terms like fruit, vegetable, meat, dairy, etc. The food_group taxonomy might be associated to a “recipe” post_type. A post of the recipe type might be titled “Fruit Salad” which could have the “fruit” term assigned to it. You should not be saying “taxonomy” (food_group) when you are talking about its terms (fruit, etc.). Individual posts have terms assigned, not taxonomies. Post_types are what are related to taxonomies, not individual posts.

    Taxonomies can be hierarchical, but it’s not required. Various terms can have parent and/or child terms. All such terms still belong to the same taxonomy. For example, the fruit term in my food_group taxonomy could have child terms such as apple, orange, pear, etc. Any of those could also have children. The apple term could have red delicious, gala, honey crisp, etc. apple varieties as children.

    Also, by default, there is only one default category — uncategorized. This can be changed, but in any case, if you are listing default categories, there would be no change from Takula term to Takula term. Defaults don’t change in the middle of a list because of a different taxonomy term.

    I don’t understand how the category terms can relate to either the Taluka taxonomy or its terms. Terms relate to posts, not other taxonomies or its terms. A post can have terms assigned from multiple taxonomies. So you could take a single Taluka term and get posts which have the term assigned. You can then take those posts and compile a list of category terms assigned to all of such posts. After stripping duplicates, the result would be similar to your example. But your code doesn’t have anything to do with posts, so I don’t think that is your intention.

    I don’t know how else one can relate terms from two different taxonomies except through posts. Your example list looks more like a hierarchical list, but term hierarchies can only be of the same taxonomy. Please describe again what you are trying to do, using proper terminology.

    Thread Starter mahendrakumbhar

    (@mahendrakumbhar)

    Well Sir, Thanks for your reply.
    Let me make you understand what i am trying to accomplish.
    Here is the link to the website. Its in Marathi a regional language of Maharashtra, India : http://ratnagiritourism.in/marathi/
    This is a tourism website. All the tourist places are added as posts with the Taluka as Taxonomy and Categories as Categories, which allow the place to be termed as a beach, waterfall, wonder, temple…etc. Just as we have districts, Taluka as region in the district assigned to Town / City/ Village..etc. Ratnagiri is a district which has several Talukas which are termed as Taxonomy and its has various categories like Beaches, Forts, Temples, Wonder, Waterfall … etc. All the districts have different categories…like the a non coastal Taluka will not have beaches but might have waterfall, wonders and Forts and all the Talukas will not have all the categories. I am trying to make an isotope filter which will display only the categories of the that Taxonomy- taluka. Here is link to that page : http://ratnagiritourism.in/marathi/filter/
    Thanks and Regards
    Mahendra Kumbhar

    Moderator bcworkz

    (@bcworkz)

    Thanks for the real world example. I do know of Marathi, though don’t I speak a single word of it. Despite it being a foreign language to me, your translations made things clear. I don’t think you realize it, but the Takula terms and category terms DO relate to each other through posts. There is no other way to relate one taxonomy to another.

    It appears that the filter for a specific Takula already works for posts, so I imagine you wish to also alter the category list so it’s limited to the categories of the posts currently shown so that visitors can further filter their Takula selection. Ideally, you should somehow acquire the list of posts that are already filtered to avoid making the exact same query again. If not, use a new WP_Query or get_posts() to repeat.

    Initiate an empty array container in which unique categories for posts will be accumulated. With a list of posts from either of the above sources, step through the list using foreach (). For each post in the list, get that post’s categories with wp_get_post_categories(). Do another foreach () for every category returned. For each category, check to see if it is already in the accumulator array. If not, add it. Repeat for all categories. Continue to the next post and do the same category process. Repeat for all posts.

    Use the resulting array to populate the category dropdown. This is all best done using Ajax technique, but reloading the entire page is feasible.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get categories of the custom taxonomy -wordpress’ is closed to new replies.