1

fromi'm using wp_list_categories to get an unordered list with headlines (on single.php inside the post-loop):

<ul><?php wp_list_categories('title_li=0');?></ul>

the thing is: i want to use it in the same way as "the_category" which means that i want to display ONLY the current categories of the post!

by now, it dsiplays ALL the categories and subcategories from the system (like it does using it in the sidebar and which makes sense there).

is there a way to do it? thanks, Jochen

EDIT: actual output (category list with ALL categories from the whole system:

<ul>    <li class="cat-item cat-item-10"><a href="#">Cat1</a>
<ul class='children'>
<li class="cat-item cat-item-18"><a href="#">Subcat1</a>
</li>
<li class="cat-item cat-item-13"><a href="#">Subcat2</a>
</li>
</ul>
</li>
<li class="cat-item cat-item-1 current-cat"><a href="#">Cat2</a>
<ul class='children'>
<li class="cat-item cat-item-12"><a href="#">Subcat1</a>
</li>
</ul>
</li>
<li class="cat-item cat-item-9"><a href="#">Cat3</a>
<ul class='children'>
<li class="cat-item cat-item-20"><a href="#">Subcat1</a>
</li>
<li class="cat-item cat-item-19"><a href="#">Subcat2</a>
</li>
</ul>
</li>
<li class="cat-item cat-item-11"><a href="#">Cat4</a>
<ul class='children'>
<li class="cat-item cat-item-15"><a href="#">Subcat1</a>
</li>
<li class="cat-item cat-item-14"><a href="#">Subcat2</a>
</li>
</ul>
</li>
</ul>

this is good so far! but the expected output i want to get is the same list as above but with just the CATEGORIES FROM THE DISPLAYED POST (not every category form the system)... i think "current" was the wrong word (sorry, i'm not a native speaker)!!

for example i open a post (single.php) with the attached category "Cat1" i want to have an output like this:

<ul>    <li class="cat-item cat-item-10"><a href="#">Cat1</a>
<ul class='children'>
<li class="cat-item cat-item-18"><a href="#">Subcat1</a>
</li>
<li class="cat-item cat-item-13"><a href="#">Subcat2</a>
</li>
</ul>
</ul>

i hope you can help! thanks...

1
  • 1
    I'm not sure how this differs from just using the_category? Can you post examples of expected output and actual output?
    – Tom J Nowell
    Feb 22, 2013 at 11:47

1 Answer 1

1

Have you tried 'current_category' => 1, ?

e.g.:

$args = array (
    'title_li' => 0,
    'current_category' => 1
);
wp_list_categories($args);

There's a chance it will still output all of them, but with a CSS class on the current categories, at which point you hide them all with CSS and unhide the ones with that class.

Always refer to the codex entry:

http://codex.wordpress.org/Template_Tags/wp_list_categories

7
  • thanks for your quick answer but the class is strangely just attached to one of the categories?!?! Feb 22, 2013 at 12:02
  • perhaps the post had only one category assigned, I'll need more information, namely expected output, and actual output
    – Tom J Nowell
    Feb 22, 2013 at 12:29
  • ..see my edits in the top-post! Feb 22, 2013 at 14:09
  • From the looks of it you have it working fine, it's showing the current cat, and there are sub categories too.
    – Tom J Nowell
    Feb 23, 2013 at 14:08
  • Perhaps using these APIs is a bad idea and you should go for the term oriented APIs and manually construct the markup based on their results. Also it would help if you screenshot red the categories on the edit screen for the post you're showing the output for.
    – Tom J Nowell
    Feb 23, 2013 at 14:09

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.