customization - Display a list of subcategories (from specific Category) a post belongs to

admin2025-06-02  1

This code almost works, but it brings other categories that I do not want to display (minimun of 1, maximun of 3). I have several categories and subcategories, that can be marked at the same post, but I just want to display subcategories that are marked and that are child of cat id=59 (example).

Code:

$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 = ', ';
$categories = get_the_category();
$parentid = $categories[0]->category_parent;
$parents = get_terms( 'category', array( 'parent' => 59 ) );
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

    $term_ids = implode( ',' , $post_terms );
    $terms = wp_list_categories( 'title_li=&style=none&echo=0&child_of=' . $parentid . '&taxonomy=' . $taxonomy . '&include=' . $term_ids );
    $terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );

    // display post categories
    echo  $terms;
}

This code almost works, but it brings other categories that I do not want to display (minimun of 1, maximun of 3). I have several categories and subcategories, that can be marked at the same post, but I just want to display subcategories that are marked and that are child of cat id=59 (example).

Code:

$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 = ', ';
$categories = get_the_category();
$parentid = $categories[0]->category_parent;
$parents = get_terms( 'category', array( 'parent' => 59 ) );
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

    $term_ids = implode( ',' , $post_terms );
    $terms = wp_list_categories( 'title_li=&style=none&echo=0&child_of=' . $parentid . '&taxonomy=' . $taxonomy . '&include=' . $term_ids );
    $terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );

    // display post categories
    echo  $terms;
}

Share Improve this question edited Feb 27, 2019 at 13:59 Omniatom asked Feb 27, 2019 at 13:27 OmniatomOmniatom 596 bronze badges 7
  • What is the significance of category 59? Do you only need the code to get children of exactly 59? Or is this value dynamic somehow? – Jacob Peattie Commented Feb 27, 2019 at 13:31
  • Hi Jacob. Yes, I need to display inside the post, a list of the subcategories of the Category (id=59), sinse the post is marked (belongs to) in those subcategories. – Omniatom Commented Feb 27, 2019 at 13:36
  • the code above brings those subcategories, but if there are less than 3, it brings other categories and subcategories I don´t want to display. Thats why I need to filter by cat 59 children only. – Omniatom Commented Feb 27, 2019 at 13:44
  • But do you only want to show children of 59 because that happens to be the category you want for this specific post? Or are you happy to hard-code 59 for all posts? – Jacob Peattie Commented Feb 27, 2019 at 13:46
  • I want to show it in a box, for all posts, like the image above. – Omniatom Commented Feb 27, 2019 at 13:58
 |  Show 2 more comments

1 Answer 1

Reset to default 1

So your problem is that you've essentially mixed up what looks like 3 separate attempts at a solution. There's a lot of redundant code in there that's making it confusing to read. The actual line of code that's generating the output that you're seeing is this one:

$terms = wp_list_categories( 'title_li=&style=none&echo=0&child_of=' . $parentid . '&taxonomy=' . $taxonomy . '&include=' . $term_ids );

The problem is that here you've set child_of=' . $parentid, but $parentid isn't set to 59, it's set to this:

$parentid = $categories[0]->category_parent;

There's no guarantee this will be the specific category that you want. The order of categories returned by get_the_category() is the order that the categories were added to the post. This isn't necessarily a category with 59 as its parent. If the first category doesn't have a parent, then this function will list all categories.

If you want:

  • List categories for the current post.
  • The categories should be children of category 59.
  • No more than 3 should be displayed.
  • The categories should be separated by commas.

Then this is all the code you need:

$categories = wp_get_object_terms( $post->ID, 'category', [ 'parent' => 59, 'number' => 3 ] );

if ( ! empty( $categories ) ) {
    $category_links = [];

    foreach ( $categories as $category ) {
        $category_links[] = '<a href="' . esc_url( get_term_link( $category ) ) . '">' . $category->name . '</a>';
    }

    echo implode( ', ', $category_links );
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748859380a314316.html

最新回复(0)