$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'); ?>loop - How do I display a list of posts under all hierarchical children of the current taxonomy?|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)

loop - How do I display a list of posts under all hierarchical children of the current taxonomy?

matteradmin9PV0评论

Assume the following custom taxonomy structure:

  • USA
    • California
      • Los Angeles
      • San Francisco
      • Oakland
    • New York
      • New York
      • Buffalo
      • Rochester

Note: It's just sample data, so don't bother to correct it!

Now, assume the following custom post type data:

  • Some posts will be country-wide, so they will only be assigned to the USA category.
  • Some posts will only be valid in certain states, so they will get assigned only those categories, eg. California or New York.
  • Some posts will only be valid in certain cities, so they will get assigned only to those categories, eg. Los Angeles or Rochester.

I want to configure WordPress such that:

  1. If I browse to a top-level category (eg. USA), it should show a list of posts that belong to USA as well as every descendant, eg. California, Los Angeles, New York, Rochester, etc., i.e. the whole country.
  2. If I browse to a mid-level category (eg. California), it should show the obvious list of posts belonging to that category as well as all descendants, i.e. California, Los Angeles, San Fransisco, Oakland, etc., but it should also show me all posts that are tagged with the top level, i.e. USA.
  3. If I browse to the last level category page, (eg. Buffalo), it should show me posts in Buffalo, it's parent, New York and it's grandparent, USA as well.

Is this something that can be configured from within the custom post type and/or taxonomy configuration or will it need modification of the loop? What exactly would I need to do to achieve this?

Assume the following custom taxonomy structure:

  • USA
    • California
      • Los Angeles
      • San Francisco
      • Oakland
    • New York
      • New York
      • Buffalo
      • Rochester

Note: It's just sample data, so don't bother to correct it!

Now, assume the following custom post type data:

  • Some posts will be country-wide, so they will only be assigned to the USA category.
  • Some posts will only be valid in certain states, so they will get assigned only those categories, eg. California or New York.
  • Some posts will only be valid in certain cities, so they will get assigned only to those categories, eg. Los Angeles or Rochester.

I want to configure WordPress such that:

  1. If I browse to a top-level category (eg. USA), it should show a list of posts that belong to USA as well as every descendant, eg. California, Los Angeles, New York, Rochester, etc., i.e. the whole country.
  2. If I browse to a mid-level category (eg. California), it should show the obvious list of posts belonging to that category as well as all descendants, i.e. California, Los Angeles, San Fransisco, Oakland, etc., but it should also show me all posts that are tagged with the top level, i.e. USA.
  3. If I browse to the last level category page, (eg. Buffalo), it should show me posts in Buffalo, it's parent, New York and it's grandparent, USA as well.

Is this something that can be configured from within the custom post type and/or taxonomy configuration or will it need modification of the loop? What exactly would I need to do to achieve this?

Share Improve this question asked Dec 17, 2018 at 10:57 aalaapaalaap 7541 gold badge8 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Use wp_list_categories with the taxonomy argument, it is built to create hierarchal category lists but it will also support using a custom taxonomy.

Example:

Display hierarchal categories

using this you can see their hierarchal structure.

let me know the result

Post a comment

comment list (0)

  1. No comments so far