Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dynamically add current-menu-item to list items
<?php $story_nav = new WP_Query( $args ); ?>
<?php if ( ! empty( $story_nav ) ) { ?>
<?php $queried_object = get_queried_object(); ?>
<?php $current_page_id = $queried_object->ID; ?>
<ul class="featured-stories">
<li>Featured Stories
<ul>
<?php while ( $story_nav->have_posts() ) { ?>
<?php $story_nav->the_post(); ?>
<?php $list_id = get_the_ID(); ?>
<li class="story<?php echo ( $current_page_id === $list_id ) ? ' current-menu-item': ''; ?>">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php } // endwhile $story_posts->have_posts() ?>
</ul>
</li>
<?php wp_reset_postdata(); ?>
</ul>
<?php } // endif ! empty( $story_nav ) ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.