I am using Advanced Custom Fields and trying to build a navigation menu where the current item has an extra class on it to let them know where they are.

The entire structure in question. It's using ACF repeater fields:

<?php if(get_field('main_nav', 53)): ?>
<?php while(has_sub_field('main_nav', 53)): ?>


$link1 = the_permalink();
$link2 = the_sub_field('link_url');

if ( $link1 == $link2 ) {?> class="s4-secondary-nav-current" <?php } ?> 

href="<?php the_sub_field('link_url'); ?>"><?php the_sub_field('link_text'); ?></a></li>

<?php endwhile; ?>
<?php endif; ?>

Specific conditional:


$link1 = the_permalink();
$link2 = the_sub_field('link_url');

if ( $link1 == $link2 ) { echo 'class="s4-secondary-nav-current"'; } else {} ?>

What seems to be happening is that $link1 and $link2 just get printed in the HTML.

Was also trying to do something like this that gets same results as above:

<?php if ( the_permalink() == the_sub_field('link_url') ) {?> class="s4-secondary-nav-current" <?php } ?> 
  • Are you using the_permalink() within the WP loop? Mar 7, 2017 at 22:42
  • No, but it outputs fine into the HTML... I used the get_permalink(); as well. They both output in test code just fine seemingly.
    – Jack Tse
    Mar 7, 2017 at 22:46
  • 1
    Use get_the_permalink as documented here: developer.wordpress.org/reference/functions/get_the_permalink . and pass in the post id. That will allow you to use the permalink value in a variable. I can't say much about the ACF function though. Mar 7, 2017 at 23:01
  • 1
    the_permalink echo's at the location in which it's executed. You cant store the result of an echo, so you have to use get_the_permalink. Mar 7, 2017 at 23:04

2 Answers 2


Both the_permalink() and the_sub_field() are functions that do more than output a string or URL.

Try usingget_permalink() and get_sub_field instead:

$link1 = get_permalink();
$link2 = get_sub_field('link_url');
  • well, that worked!
    – Jack Tse
    Mar 7, 2017 at 23:06
  • Glad to hear it! Mar 8, 2017 at 23:44

the_permalink doesn't do what you think it does.

For example, this:

if ( the_permalink() == 'test' ) {

Is the same as:

echo get_the_permalink();
if ( '' == 'test' ) {

the_permalink doesn't return a value, and outputs it directly. Use get_the_permalink instead, and check what functions do before using them.

  • Thank you, Tom. I'm newish on WP after years on another CMS.Is this the best way to apply the class to the current link on the nav?
    – Jack Tse
    Mar 7, 2017 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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