Change class based on what category has been searched
I use the following code to highlight a menu item if the post is in this category or the category is selected:
<a href="/category/presets/photoshop_actions" title="Only Photosshop Actions"
<?php if (is_category(photoshop_actions) || is_single() && in_category('47')) {
echo ' class="rounded rounded_active" ';}
else{
echo ' class="rounded" ';}
?>
>
<span>
Photoshop Actions
</span>
</a>
开发者_如何学编程
So here is the deal: I also want to have the rounded_active class set if there has been performed a search in the category - lets say the url looks like this when the search has been performed: http://localhost:8888/?cat=47&s=boats How do I expand the code above to check if category with id 47 is part of the search query and then echo back
class="rounded rounded_active"
<?php $active = is_category('photoshop_actions') || in_category(47) || get_query_var('cat') == 47; ?>
<a href="/category/presets/photoshop_actions" title="Only Photosshop Actions" class="rounded<?php echo $active ? ' rounded_active' : ''; ?>">
<span>Photoshop Actions</span>
</a>
The PHP statement within the class attribute is a shorthand if
.
I also kept the main logic outside of the HTML to keep things a little neater.
精彩评论