I'm working on displaying a series of child categories in the meta of each post. For instance, after the post appears an array, depending on category selection, describing the post content:

Color: Red | Shape: Circle | etc.

The following code displays the content as desires except not in the correct order. The display currently appears as alpha order of the child categories.

I need to either order the categories by parent ID or in the order their presented in the code.

Any ideas?

<span class="cat-links">
    foreach((get_the_category('orderby=ID&order=ASC')) as $childcat) {
    if (cat_is_ancestor_of(3, $childcat)) {
    echo 'Color: '. '<a href="'.get_category_link($childcat->cat_ID).'">';
 echo $childcat->cat_name . '</a> | ';
    if (cat_is_ancestor_of(10, $childcat)) {
    echo 'Shape: '. '<a href="'.get_category_link($childcat->cat_ID).'">';
 echo $childcat->cat_name . '</a> | ';
  • "I need to either order the categories by parent ID or in the order their presented in the code." ??? – kaiser Feb 3 '12 at 0:30
  • Yes, the current orderby param is affecting the child. If a post has Color: Blue and Size: Small, Color is listed first and Size next. If a post has Color: Red and Size: Large, then Size is displayed first then Color. The two ways I specified are possibilities that occurred to me. I need to be able to consistently display category order according to the parent, however it's done. – dooley Feb 3 '12 at 4:58
  • Ehm... Why don't you just exchange the code blocks? – kaiser Feb 3 '12 at 5:07
  • I tried that. Same result. – dooley Feb 3 '12 at 6:39
  • Do a var_dump of the inputs of your foreach loop and link to a pastebin. – kaiser Feb 3 '12 at 6:49

If you install Category and Taxonomy Terms Order you get a nice drag and drop interface and all you have to do to call it is to set orderby => 'menu_order' and you're done, your drag and drop category changes will be instantly reflected in your category order.

  • I tried this plugin and it hasn't effected the order of categories in post meta. I add orderby => menu_order to the get_the_category tag, placing the current param, yes? – dooley Feb 3 '12 at 4:37
  • Should be menu_order...check the documentation, it's pretty good. – mor7ifer Feb 4 '12 at 3:37
  • menu_order, I figured out. Looks like a helpful plugin. It this situation, though, it's not working. (I checked in a live sandbox and my XAMPP setup.) From what I tell, the param in get_the_category is applying to the child since it's getting as $childcat. – dooley Feb 4 '12 at 18:14

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.