Run a conditional check in the foreach
loop using is_category($term-name)
Assign a class variable to active
if it’s the same as $term->name
$terms = get_terms( 'category' );
echo '<ul>';
foreach ( $terms as $term ) {
$class = ( is_category( $term->name ) ) ? 'active' : ''; // assign this class if we're on the same category page as $term->name
echo '<li><a href="' . get_term_link( $term ) . '" class="' . $class . '">' . $term->name . '</a></li>';
}
echo '</ul>';
Related Posts:
- Show scheduled posts in archive page
- have_posts() return false but count says “3”
- Get posts that matches specific terms of multiple custom taxonomies
- Using get_term() in functions.php results in Invalid taxonomy error
- How do you split multiple-column loop on category archive page?
- Displaying terms based on loop posts?
- Duplicated posts on category page
- Order archive results by post id in custom taxonomy template
- How to order posts by title after they have already been sorted by category
- Default ‘post’ post type archive slug?
- For each loop on every word in post
- First archive page with a few posts
- Query Nopaging action not having effect
- add offset to archive page
- Modify loop output for a specific list of categories
- Show terms in archive page
- Avoid WP_Query’s duplicate posts with taxonomies
- Is it possible to set archive.php instead of index.php to display blog?
- How to add pagination in Terms
- Duplicate posts on archive page
- Exclude posts from loop by term
- Archive: Lists itself
- Archive sorted by month – separate
- Creating a archive for taxonomy terms, not the term results
- wordpress showing all posts instead of date range
- Modify my code – which takes the first sentence of the post and use it as a h2 tag – to work outside the loop
- How to get Author ID outside the loop
- remove tags from the_content
- the_title() shows title of the first post instead of the page title?
- How do I prevent one of two multiple loops from repeating on a second page?
- What exactly defines a Main Loop and a Secondary Loop?
- Loop code is displaying pages but not actual posts
- How are both HTTP and HTTPS versions displaying?
- post loop causes wp_users and wp_usermeta DB queries for each users
- Ajax loop refresh on click
- How to Loop within a Loop (Display Children and then Grandchildren)
- the_excerpt(), get_the_excerpt() and the_content() all killing “the Loop”
- Loop through pages with specific template
- Loop starts from the beginning with second while loop on author.php
- wordpress change the loop order by dynamic value
- Display content of page template (get_page)
- Single page site (WordPress) including posts
- Meta Query with spaces in value?
- A loop with all articles with certain text strings in tags
- Display the_content on click jquery
- How can I create a legend (Key) like the one in the footer on WordPress StackExchange
- Missing image in gallery shortcode in custom feed
- Styling text and images in the_content()
- Including post thumbnail url in loop’s post object to reduce number of queries
- How do I use AJAX Query in my posts loop?
- Duplicate posts in paginated results
- Output loop leveraged shortcode twice on one page
- Using Javascript for Looped Content
- Added if statement to loop
- How to get The Loop working with $wpdb->get_results()
- Making a query to the DB using same parameters of loop
- Display Posts Only with Specific Tag
- How to get my loop to paginate?
- Group posts by date with a list
- Add class to last 3 posts in loop
- Will only show the first 10 wordpress posts instead of all my posts
- WP Loop. If featured image is a panorama (3:1 ratio) execute some code
- Count post views in loop
- displaying recent comments outside loop without plugin
- get_the_ID not returning correct ID value
- Include future posts in tags and in search
- Separate First Post Loop
- loop inside the foreach
- make get_post work in the loop
- How to display 3 post per slide
- How to put a block of code between [shortcode][/shortcode] in do_shortcode()?
- current-user_can not working in loop
- How can I access a post’s feature image outside the loop?
- Creating ‘posts page’ loop based on the page itself
- Multiple loops for plugin
- get_the_terms() returning wrong results inside of loop
- How to read taxonomy in loop?
- Undefined index: page in loop load ajax content
- Only Show Excerpt After First 3 Posts
- How to get all multi-select user meta values and add them to an array?
- How to insert content before pagination in loop?
- How to resolve a reload loop issue in the frontend when logged in to WordPress multi-site backend admin area
- Excerpt isn’t working or am I missing something?
- Deeply Nested Menu Loop with Twig using Timber [closed]
- WordPress redundant posts based on meta field
- Get featured image outside the loop using foreach
- Infinite Loop after Genesis Theme Upgrade
- pre_get_posts main_query not displaying after applying tax_query filters
- setup_postdata in “loop-handling” php file called via $.ajax
- Secondary loop cuts off at 10 posts?
- get all page templates
- Paged homepage – requirements?
- strange parse error when including a loop template within another template
- Previous Posts Link and Next Posts Link Show All The Same Articles?
- Show all posts in category
- Posts Loops To Display Specific Posts Inside Page Template?
- Use callback function in AJAX pure javascript [closed]
- the_excerpt() not working in custom archive
- Undefined variable: woocommerce_loop [closed]
- title not being printed within html tag [closed]