$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Print the associated categories of the current post starting with parent (with option to remove href)|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Print the associated categories of the current post starting with parent (with option to remove href)

matteradmin12PV0评论

Not sure why this seemingly elementary function is so difficult to perform, but I've spent a lot of time scouring the web, incorporating the suggestions and not getting the intended output.

On the single post template I would like to display the categories like this:

Parent, Child, Child

If there are no children, it will just be:

Parent

I would also like the option to remove the href. Currently I have:

<?php
  $cat = get_the_category();
  $allCats = $cat[0]->cat_ID;
  echo get_category_parents( $allCats, false, ',' ); 
?>

This doesn't work in two ways:

1) It's missing one of my 3 categories

2) It's adding a comma after the last category (there should be 3rd a category here)

The standard code:

 <?php the_category(', ')?>

prints all three categories, but I don't have the option to strip the href, and they are in this incorrect order:

Child,Parent,Child

I'm guessing there will be a super simple way to do this but it's not super simple to find online! Any help appreciated :)

Not sure why this seemingly elementary function is so difficult to perform, but I've spent a lot of time scouring the web, incorporating the suggestions and not getting the intended output.

On the single post template I would like to display the categories like this:

Parent, Child, Child

If there are no children, it will just be:

Parent

I would also like the option to remove the href. Currently I have:

<?php
  $cat = get_the_category();
  $allCats = $cat[0]->cat_ID;
  echo get_category_parents( $allCats, false, ',' ); 
?>

This doesn't work in two ways:

1) It's missing one of my 3 categories

2) It's adding a comma after the last category (there should be 3rd a category here)

The standard code:

 <?php the_category(', ')?>

prints all three categories, but I don't have the option to strip the href, and they are in this incorrect order:

Child,Parent,Child

I'm guessing there will be a super simple way to do this but it's not super simple to find online! Any help appreciated :)

Share Improve this question asked Feb 25, 2019 at 13:23 Sean DohertySean Doherty 1115 bronze badges 1
  • Anonymous markdowns are so useful, well done! – Sean Doherty Commented Mar 21, 2019 at 14:28
Add a comment  | 

1 Answer 1

Reset to default 0

Here is code snippet from WordPress that display the categories (or terms from other taxonomies) assigned to a post ordered by parent-child category relationship.This example must be used inside the loop.

$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;
}

Information on this page may help you to adjust the code to your requirements. I hope this will help.

Post a comment

comment list (0)

  1. No comments so far