$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'); ?>categories - Display a list of subcategories a post belongs to|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)

categories - Display a list of subcategories a post belongs to

matteradmin10PV0评论

I have this code:

<?php
// List subcategories of category '4'
$subcategories = get_categories('&child_of=4'); 
foreach ($subcategories as $subcategory) {
  echo sprintf(' <a href="%s">%s</a> <span class="sep">•</span>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
}
?>

This code retrieves a list of subcategories from category id 4. I have added this code in post loop(have_posts()). It is showing all tagged subcategories from that category on every post's box.

What I want is to just show the subcategories from the category 4, which is tagged in that post. It shouldn't show all the subcategories from all categories tagged for all the posts.

Thanks in advance

I have this code:

<?php
// List subcategories of category '4'
$subcategories = get_categories('&child_of=4'); 
foreach ($subcategories as $subcategory) {
  echo sprintf(' <a href="%s">%s</a> <span class="sep">•</span>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
}
?>

This code retrieves a list of subcategories from category id 4. I have added this code in post loop(have_posts()). It is showing all tagged subcategories from that category on every post's box.

What I want is to just show the subcategories from the category 4, which is tagged in that post. It shouldn't show all the subcategories from all categories tagged for all the posts.

Thanks in advance

Share Improve this question edited Jun 9, 2014 at 12:57 Pieter Goosen 55.5k23 gold badges117 silver badges211 bronze badges asked Jun 9, 2014 at 11:08 user57867user57867 12 silver badges4 bronze badges 2
  • Your english is a bit broken, making it hard to understand. Do you just want to show posts from category 4 on the homepage? – Pieter Goosen Commented Jun 9, 2014 at 11:26
  • @PieterGoosen Sorry for my bad english. i am updated the question. my problem is, It showing all the subcategories tagged in for all posts. i just want to show only the subcategories tagged for that post. see this (prntscr/3r32oy). it is showing all subcategories tagged in for all posts. i just want to show only the subcategories tagged for that post – user57867 Commented Jun 9, 2014 at 11:54
Add a comment  | 

2 Answers 2

Reset to default 2

Here is a function I've recently used (modified version of the code found in the codex) to display a list of the categories a post is attached to.

This function first gets the parent category to which the post belong, and that info is then fed back into wp_list_categories to remove the parent category and to get a list of the child categories belonging to that parent

<?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 = ', ';
$categories = get_the_category();
$parentid = $categories[0]->category_parent;

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

To do so, you should remove the ampersand (which is used to concatenate multiple arguments) and use parent instead of child_of, so that only subcategories (and not deeper-nested categories) are listed:

$subcategories = get_categories( 'parent=4' );
Post a comment

comment list (0)

  1. No comments so far