I have a category called menu items (category id 19) and two child categories Tacos and Burgers. Why does Tacos get echoed out twice? I get Tacos Tacos Burgers.

while (have_posts()) : the_post(); 
    foreach((get_the_category()) as $childcat) {
        if (cat_is_ancestor_of(19, $childcat)) {
            echo $childcat->cat_name;
  • it assumes you have 2 posts having "tacos" category and 1 post with "burgers" category assigned. Aug 19, 2016 at 0:33

1 Answer 1


Because you are echoing the $childcat->cat_name each time a post passes the validation.

Remember that you are inside the loop and for each post that matches your condition your are echoing. You seem to be wanting to print a list of categories that are a child of 19 but I don't see why you'd go over each post to do that.

I would give you a solution but I'm not sure where you are going with this, if you share what you are trying to accomplish I will update my answer

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.