Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I cannot figure out how to use the wp_list_categories function to do what I need it to:

<?php if(is_page('realisations')) { ?>
<ul id="subpage">
    <li><strong>Les secteurs</strong></li>
    <?php wp_list_categories('child_of=3&title_li='); ?>
</ul>
<?php }; if(is_category() or is_single()) { ?>
<ul id="subpage">
    <li>Les secteurs</li>
    <?php wp_list_categories('child_of=3&title_li='); ?>
</ul>
<?php }; ?>

I hope this doesn't come off as too convoluted. It does what I need it to do, despite not being very elegant. I have it set up so that all of the project categories are child categories of the a main category (child_of=3) This is to avoid conflicts with the news section of the site.

The problem is that I need the category the post is in to highlight when viewing the single.php page template, but I don't know how to accomplish that. When on a category page the category view highlights correctly because I styled the class that WordPress adds into the generated list .current-cat.

This is a crosspost from the normal StackExchange site, it was suggested that I submit the problem here.

share|improve this question
    
You're right, the current-cat class is only used when displaying a category page. As a post can be within multiple categories, this is harder to find out. Will do some checking... –  hakre Nov 19 '10 at 19:59

2 Answers 2

I had a similar question, and the solution was to add a function in functions.php.

How to get current_cat in navbar, in single post

share|improve this answer

Okay, here is how to do that and - for further reference - some infos how you can get the most of these wordpress functions:

If you're using a function like the one you used in your code, you should checkout the according codex documentation page: wp_list_categories().

Often these function have multiple parameters with which you can control what the function is doing.

Gladly for what you ask for, a parameter already exists: current_category. You can make use of it to signal what the current category is. Probably this is how it could look like:

<?php if(is_page('realisations')) { 
    // get the first category id of the post/page
    $category = (int) reset(get_the_category());
?>
<ul id="subpage">
    <li><strong>Les secteurs</strong></li>
    <?php wp_list_categories('child_of=3&current_category='.$category.'&title_li='); ?>
</ul>
<?php }; if(is_category() or is_single()) { ?>
<ul id="subpage">
    <li>Les secteurs</li>
    <?php wp_list_categories('child_of=3&title_li='); ?>
</ul>
<?php }; ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.