• Hi all,

    I’m attempting to put together a website, but categories aren’t working the way I want them to. Notice that on my site currently, there’s a query loop that pulls posts from the Airchecks category, and another that pulls posts from the Commentaries category. The plan is that each post will be assigned to a child category within one of these two parents. For instance, if I’m posting airchecks from Portland, Seattle, Orlando, and Spokane, each of those cities will be its own category, with then each station’s callsign being what the post is tagged with. The thinking now is that in the sidebar underneath the two query loops, I’d have headings that say top markets and top stations. The problem is that I can’t seem to find a way to make the categories list only show child categories of the Airchecks category. Below that is to be a tags list, which should be pretty easy to do assuming we can get the categories to work correctly. Is there a way to do this?

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    AFAICT we can only list children of a single parent, not all children. At least by using available WP_Term_Query args. You could directly alter the SQL WHERE clause to only get terms whose parent property does not equal zero. This will return all child terms. You can use the ‘terms_clauses’ filter to alter the SQL used. All term queries pass through this filter, so you’d need to be sure you’re only altering the correct query and not any others.

    Alternately, build your own list by directly querying for terms using global $wpdb object methods. This becomes more difficult if you require a hierarchical list, but if it’s a flat list of only immediate child terms this is pretty feasible to do.

    Thread Starter bob cavanaugh

    (@bobdavcav)

    That’s what I want to do, only list the child categories of one parent. Currently, the categories list is showing all parents and children, which is not what I want.

    Moderator bcworkz

    (@bcworkz)

    Ah! That we can do. Apologies for my misunderstanding. You’ll need to determine the ID of the parent term of whose descendants you want listed. Then you could place code similar to this on your sidebar template:

    wp_list_categories([
    	'title_li' => 'Preferred Title',
    	'child_of' => 123,
    	'taxonomy' => 'category',
    ]);

    Pass an empty string for title_li to suppress any sort of title. 123 is the parent category term’s ID in this example, use the actual ID. This only works in templates for classic themes. If you have a block theme, or you want to use this as a widget, this code will need to be worked into a custom shortcode.

    There are all sorts of other parameters you could pass to fine tune the output. Check out the doc page for the function.

    To do similar for tags, remove the child_of line and change “category” to “post_tag”

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘categories list with only child categories’ is closed to new replies.