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

admin2025-06-05  3

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749091514a316289.html

最新回复(0)