0

I making some updates to a theme I'm currently working on.

I've moved a bunch of categories that have custom post items under them into a parent category that's all within a custom taxonomy.

e.g.:

Parent Category
-Child Category
--Item
--Item
--Item
-Child Category
--Item
--Item
-Child Category
--Item
--Item
--Item

I'm able to list them out on a taxonomy-custom-tax-name.php page, but when I click on the link of the parent category, it's lists all of the items that are associated the children category and not just the children category themselves.

e.g.:

--Item
--Item
--Item
--Item
--Item
...etc.

I'm trying to get it so it just lists out the children categories with a link to then go to a page with the items attached to that category:

e.g.:

-Child Category
-Child Category
-Child Category
-Child Category

I'm currently using this loop on the custom taxonomy page:

<?php global $query_string; query_posts( $query_string . '&posts_per_page=-1' ); if (have_posts()) : while (have_posts()) : the_post(); ?>

I'm not sure if I need to change up my loop statement or if I need to create a if else case statement?

I've already tried creating separate custom taxonomy pages as well as a separate category page with the parent category name and it's still not working the way I'm try to get it to.

I hope that makes sense. This is the first time for me really diving into custom taxonomies so I'm trying to wrap my head around the logic and it seems like there is more than one way to get everything to display correctly.

1

Two things!

1) Don't use query_posts()! Instead, for situations like this, use the pre_get_posts action to modify the query.

2) I don't think you actually want to be using a loop here at all (so neither query_posts() nor pre_get_posts or even WP_Query). Rather, I think you're looking for wp_list_categories. In your template, you'd probably do something like this (untested):

<?php
// the taxonomy archive term page's term id
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;

$list_child_terms_args = array(
    'taxonomy' => 'YOUR_CUSTOM_TAXONOMY_SLUG_HERE',
    'use_desc_for_title' => 0, // best practice: don't use title attributes ever
    'child_of' => $term_id // show only child terms of the current page's
);
?>
<ul>
    <?php wp_list_categories( $list_child_terms_args ); ?>
</ul>
0
<?php  
$categories = get_categories();      // Current Category will retrive
foreach ( $categories as $category ) {
$cat = $category->name;              // Parent Category name
$category = get_category_by_slug( $cat );

$args = array(
'type'                     => 'post',
'child_of'                 => $category->term_id,
'orderby'                  => 'name',
'order'                    => 'ASC',
'hide_empty'               => FALSE,
'hierarchical'             => 1,
'taxonomy'                 => 'category',
); 

$child_categories = get_categories($args );

$category_list = array();
$category_list[] = $category->term_id;

if ( !empty ( $child_categories ) ){    // If child Category available
    foreach ( $child_categories as $child_category ){    // Print Child Category
        $category_list[] = $child_category->term_id;
        echo "main cat:". $cat; echo "sub cat:". $child_category->cat_name."<br/>";
    }
}
}

Paste to your php file to get sub categories...

  • 2
    This is considered code dumping; please explain your answer in detail so that people can understand and use it. Add an explanation of what your code does, and comment your code. – engelen Mar 28 '16 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.