1

I'm building a branching navigation that displays pages, children pages and a custom taxonomy (named region/s).

Everything works as intended except in any instance where a region/page have the same ID. Not sure why this would happen as I'm under the impression IDs are unique but its happening and it displays the incorrect link in the navigation.

I know where my code is failing to fix the issue, I'm just not sure how to fix it. Doing a bit of searching has left me empty handed in WP docs on how to tell the difference between a Page and a Custom Taxonomy.

Code below:

/** Creates Menu Link Markup
* This is where most issues happen. 
* In any instance that I check an ID if it exists as a page or taxonomy
* It'll output as whichever it checks first, in this case a tax.
*/  
function menu_link_markup( $id ) {
    var_dump(term_exists($id) );

    if ( get_term_by( 'id', $id, 'regions' ) ) {
        $anchor_title = get_term_by( 'id', $id, 'regions' )->name;
    } else {
        $anchor_title = get_the_title( $id );
    }


  $category_link = get_category_link( $id );
  if ( !empty( $category_link ) ) {
    $anchor_href = get_category_link( $id );
  } else {
    $anchor_href = get_permalink( $id );
  }

  return '<a href="' . $anchor_href . '">' . $anchor_title .'</a>';
}



/** This just check if any of the pages in the menu
* have a regions ACF field. 
* If it does, then grab the regions, and create an array
*/ 
function get_sub_menu_regions( $page_id ) {
    $has_regions = get_field( 'regions_display_option', $page_id );

    if ( empty( $has_regions ) ) return false;

    $world_regions = [];

    $regions = get_terms( array(
      'taxonomy' => 'regions',
      'parent' => 0
    ) );

    foreach ($regions as $region) {
      $world_regions[] = [
        'id' => $region->term_id
      ];
    }

    return $world_regions;
}


/** Just checks which page is current. And if a parent or grandparent is current. 
* 
* 
*/ 
function active_menu_item_id( $id, $parent_id, $current_page ) {
    foreach (get_post_ancestors($current_page ) as $parent_page_id) {
        if ( $parent_page_id === $id || $id == $current_page || $id == $current_page->post_parent ) {
            return true;
        } 
    }

    return false;
}


/** Builds the tree array for the navigation
* 
* 
*/ 
function build_tree(array $elements, $parentId = 0, $page_id ) {

    $branch = [];

    foreach ($elements as $element) {

            $element['active'] = active_menu_item_id( $element['id'], $element['parent_id'], $page_id );

            $element['regions'] = get_sub_menu_regions( $element['id'] );

        if ( $element['parent_id'] == $parentId ) {

            $children = build_tree( $elements, $element['id'], $page_id );

            if ( $children ) {
                $element['children'] = $children;
            }

            $branch[] = $element;
        }
    }

    return $branch;
}


/** Recursive function that creates the markup from the 
* nav array created from the build_tree function. 
* 
*/     
function build_menu_list( array $nav ) {


    if ( !empty( $nav ) ) {

        foreach ($nav as $item) {

            $active = $item['active'] ? ' current_page_item ' : '';

            ?>

            <li class="page_item page-item-<?php echo $item['id']; echo $active; ?> ">

                <?php echo menu_link_markup( $item['id'] ); ?>

                <?php if ( !empty( $item['children'] ) || !empty( $item['regions'] ) ): ?>

                        <ul class="children <?php echo !empty( $item['regions'] ) ? 'regions' : '';  ?>">
                            <?php 

                                if ( !empty( $item['children'] ) ){
                                    build_menu_list( $item['children'] );
                                }

                                if ( !empty( $item['regions'] ) ){
                                    build_menu_list( $item['regions'] );
                                }

                            ?>      
                        </ul>

                <?php endif; ?>

            </li>

            <?php
        }

    }
} 
  • 1
    All terms have unique IDs, and all pages have unique IDs, but they are different things in different tables, so those IDs are not unique between the two. You can not reliably determine which is which by ID alone, you need to rethink your approach. – Milo Feb 23 '17 at 4:52
  • What is the $nav argument in the build_menu_list function? – Jon Feb 23 '17 at 13:16

Your Answer

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

Browse other questions tagged or ask your own question.