0

I am building my own custom menu for custom post type. I have a loop and I need to know if given item is active one.

How to this properly? How to compare if given items address (slug, permalink, id or some other appropriate option) is the one that is currently loaded?

$args = array('post_type' => 'services');
$services = new WP_Query( $args );
$cnt = 0;
if( $services->have_posts() ) {
    while( $services->have_posts() ) {
    $services->the_post();
    // HOW TO CHECK IF $services is the active page?
1

Use get_the_ID() before you start the loop, then compare:

$original_id = is_singular() ? get_the_ID() : 0;

$args = array('post_type' => 'services');
$services = new WP_Query( $args );
$cnt = 0;
if( $services->have_posts() ) {
    while( $services->have_posts() ) {
    $services->the_post();
    if ( get_the_ID() === $original_id )
        echo 'This is me!';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.