How to determine the current post with get_posts()
-
I was able to use get_posts() and a foreach loop to generate an unordered list of links using the_permalink & the_title for a CPT that I’m including on single-cpt.php to link to each of the CPTs. However, I can’t figure out how to add a class to the link of the current cpt by identifying which cpt I’m on.
I have single-cpt-1.php, single-cpt-2.php, single-cpt-3.php and single-cpt-4.php and the list of links renders on each. If I’m on single-cpt-2.php I’d like to style that link differently than the others. Is this possible using get_posts?
<?php // get solution cpt's to create the link bar $posts = get_posts(array( 'post_type' => 'solution', 'posts_per_page' => -1, 'meta_key' => 'posts_order', 'orderby' => 'meta_value', 'order' => 'ASC' )); if ($posts) : ?> <!-- links to solutions --> <div class="navigation-bar-wrap container--narrow"> <ul role="navigation" aria-label="navigation-bar" id="navigation-bar" class="navigation-bar"> <?php foreach ($posts as $post) : setup_postdata($post) ?> <li class="navigation-bar-li"> <a class="btn btn--blue hvr-push navigation-bar-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endforeach; ?> </ul> </div> <?php wp_reset_postdata(); ?> <?php endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘How to determine the current post with get_posts()’ is closed to new replies.