I have a little script that display any categories that contain posts and displays them (like a little menu)
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
echo '<a class="blog-panel-cat-menu bg-color-1" href="' . get_permalink( get_option( 'page_for_posts' ) ) . '">All</a>';
foreach( $categories as $category ) {
if ($category->count > 0){
$category_link = sprintf(
'<a class="blog-panel-cat-menu bg-color-1" href="%1$s" alt="%2$s">%3$s</a>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name )
);
echo $category_link;
}
}
?>
When clicking on any link it will redirect to a page that show the posts for that category eg mywebsite/news/category/blog/
or mywebsite/news/category/news/
(mywebsite/news
being the blog homepage which displays all categories)
When I'm on mywebsite/news/category/blog/
I want the "blog" link in the menu to have the class blog-cat-focus
after the current class in the foreach loop like this <a class="blog-panel-cat-menu bg-color-1 blog-cat-focus" href="%1$s" alt="%2$s">%3$s</a>