Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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!';
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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