Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress: Add a class on an li for “current category”

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #43029
    Noel Forte
    Participant

    Hi-
    Currently I’m using this code in WordPress to display a string of breadcrumbs (Link / Link / Link) across the top of my archive pages via twitter Bootstrap.

    $cat_arguments = array(
    ‘orderby’ => ‘name’,
    ‘parent’ => 0
    );
    $cats = get_categories($cat_arguments);
    $i = 0;
    foreach($cats as $category) {
    echo ‘<li><a href=”‘.get_category_link( $category->term_id ).'”>’.$category->name.'</a>’;
    $i++;
    if ($i < count($cats)) {
    echo ‘/‘;
    }
    }
    ?>
    However, I want to add a `class=”current-category”` to the `

  • ` that contains the anchor for the currently displaying category page. What’s the best way to do this?
#126475
Noel Forte
Participant

After experimenting for several hours, I was able to find something that worked.

#126496
Alen
Participant

Can you share your solution, so other people can get some ideas if they run into similar issues.

#127549
Noel Forte
Participant

I would, but unfortunately, I’m having trouble dealing with having to escape the <> symbols. =(

#127551
Alen
Participant

You could create CodePen, in the HTML section just comment out any php code, like so http://codepen.io/anon/pen/rybqo

#127995
Noel Forte
Participant

Ok, so here’s what I was able to come up with. Basically using WP’s conditional tags, I created an IF statement that checked to see if the current category was not equal to the ID of the current category. Therefore, if there was a match for ID’s, we could then run a separate line where we added a class to the current category.

Here’s the [pen](http://codepen.io/anon/pen/LCHFJ “codepen!”).

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.