Support

Account

Home Forums ACF PRO Having current_menu_item when using a relationship field Reply To: Having current_menu_item when using a relationship field

  • Hi @abbasinho

    One way to accomplish that is to use the WP function get_queried_object_id

    With that you can grab the Post/Page ID of the current page before you start iterating through your loop.

    Then, in your loop you can match $nav_item->ID with the returned value of get_queried_object_id then you can apply the class appropriately.

    In my testing, I set a variable containing the class name at the top of the loop if there was a match, then reset the variable at the end of the loop. There are a bunch of different ways, but using get_queried_object_id is key.

    I realize this is a late reply, but hopefully it’s helpful to you or someone else.

    Here’s my test code:

    <?php
    $current_page_id = get_queried_object_id();
    $class = '';
    $posts = get_field('relationship', 23);
    if( $posts ) {
      $content .= '<ul>';
      foreach( $posts as $p ) {
        if ( $current_page_id == $p->ID ) {
          $class = ' style="text-decoration:line-through"';
        }
        $content .= '<li>';
        $content .= '<a href="' . get_permalink( $p->ID ) .'"' . $class . '>' . get_the_title( $p->ID ) . '</a>';
        $content .= '</li>';
        $class = '';
      }
      $content .= '</ul>';
    }
    echo $content;
    ?>