Your cart is currently empty!
Get Category in WP – Parent, Link, Subcategory, ID, Name, List
If you are designing website or need to call a category list into website then here is something for you, How to Get Category List in WP Parent, Subcategory. Same issue I’m facing but at last resolved by searching and trying lots of codes.
Get all category of specific post
Below code helps you to get category of specific post but you must add this code into content-single.php or single.php whatever theme using for single post loop.
<?php $taxonomy = 'category'; // Get the term IDs assigned to post. $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); // Separator between links. $separator = ', '; if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) { $term_ids = implode( ',' , $post_terms ); $terms = wp_list_categories( array( 'title_li' => '', 'style' => 'none', 'echo' => false, 'taxonomy' => $taxonomy, 'include' => $term_ids ) ); $terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator ); // Display post categories. echo $terms; } ?>
Get Subcategory from Parent Category of particular post
Get subcategory from parent category in particular or specific post then use below code in post template where index and archive post loop start, you can also post after title as well, just change category ID in code or you specific category. [you can get ID from category page when you are editing any category see ID in url link ]
<?php $taxonomy = 'category'; // get the term IDs assigned to post. $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); $post_terms_ex = array (1); $result_post=array_diff($post_terms, $post_terms_ex); // separator between links $separator = ', '; if ( !empty( $result_post ) && !is_wp_error( $result_post ) ) { $term_ids = implode( ',' , $result_post); $terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids ); $terms = rtrim( trim( str_replace( '', $separator, $terms ) ), $separator ); // display post categories echo $terms; } ?>
Get Category Name By ID
If you know the ID of category you can get it’s name here is code.
<?php //Method 1 $namebyid= '1'; echo get_the_category_by_ID($namebyid); //Method 2 echo get_cat_name(1); ?>
Get Category Description By ID
Category slug is URL name of that category to get the category is go to dashboard then posts then categories now edit any category when page opened you can see category id in browser addess bar into URL
<?php $desbyid = '1'; echo category_description($desbyid); ?>
Get Category Description By Slug
Category slug is url name of that category
<?php echo category_description( get_category_by_slug('uncategorized')->term_id ); ?>
Get Category Link Using ID
You can generate a link to particular category using id with below code
<?php $linkbyid = '1'; $categorylink = get_category_link( $linkbyid ); echo esc_url($categorylink); ?>
Get Post From Specific Category Using ID, Name
If you want post from a specific category you can use below code for more info visit Codex
<?php if ( in_category('fruit') ) { include 'single-fruit.php'; } elseif ( in_category('vegetables') ) { include 'single-vegetables.php'; } else { // Continue with normal Loop if ( have_posts() ) : while ( have_posts() ) : the_post(); // ... } ?>
I have added these code to help you and find at one place, Please leave comment or suggestion below
Comments
One response to “Get Category in WP – Parent, Link, Subcategory, ID, Name, List”
I need to display all subcategories of the custom post type. How can I do it?
Custom post type: productos
Taxonomy: categoriasproductos
Thanks
Grabber Pro
Original price was: $59.$39Current price is: $39.Insertcart Custom WooCommerce Checkbox Ultimate
Original price was: $39.$19Current price is: $19.Android App for Your Website
Original price was: $49.$35Current price is: $35.Abnomize Pro
Original price was: $30.$24Current price is: $24.Medical Portfolio Pro
Original price was: $31.$24Current price is: $24.
Latest Posts
- How to Handle Sudden Traffic Spike in Website – Do Node Balancer Really Help
- How to Use AWS SES Email from Localhost or Website: Complete Configuration in PHP
- How to Upload Images and PDFs in Android Apps Using Retrofit
- [Fix] File Permission Issue in Apache Website WordPress Not Writable to 775
- Best PHP ini Settings for WordPress & WooCommerce: Official Recommendations
Leave a Reply