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 category wordpress

 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

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website