Search code examples
wordpresstaxonomy

Wordpress: Display Subcategory Name Without Link


I have this code that works great to show the subcategory name and link of a specific parent category of my choosing:

<?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
$categories = get_the_category();
$parentid = '6';

if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

$term_ids = implode( ',' , $post_terms );
$terms = strtr( wp_list_categories( 'title_li=&style=none&echo=0&child_of=' . $parentid . '&taxonomy=' . $taxonomy . '&include=' . $term_ids), array( '<br />' => ' <br /> ' ) );
echo preg_replace( '@\s<br />\s\n$@', '', $terms );
}
?>                            

Now I want to be able to do the same thing but WITHOUT the code above generating the link automatically. Any ideas?


Solution

  • Use get_categories() instead of wp_list_categories()

    $terms = get_categories("child_of={$parentid}&include={$term_ids}");
    
    foreach($terms as $term)
        echo $term->name;