• Resolved fettabachi

    (@fettabachi)


    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)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    See https://developer.wordpress.org/reference/functions/get_the_id/ then add the right “current” class to your menu link, probably “current-menu-item”.

    Yes, you’ll want to get the id when you’re on that page you are placing nav links to all these solutions (custom post type posts) and then mark the solution you’re currently on. Outside your get_posts loop you can call $thisid = get_the_ID(); and then inside your loop you check that post id and see if it’s equal to the $thisid

    Something along these lines:

    // get this post id
    $thisid = get_the_ID();
    
    // get solution cpt's to create the link bar
    $posts = get_posts( array(
        'post_type'   => 'solution',
        'numberposts' => -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 <?php
    					// check this post vs current post
                        if ( $thisid === get_the_ID() ) {
                            echo 'current';
                        }
                    ?>">
                        <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; ?>
    Thread Starter fettabachi

    (@fettabachi)

    This solved my problem!

    Thanks to both of you @sterndata & @circlecube. In my mind I thought I needed to pull the ID from the get_posts(array).

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.