<div class="location-nav d-flex justify-content-around my-3">

        <?php

        if( is_page() && $post->post_parent > 0 ) {
      // post has parents

      $children = get_pages('child_of='.$post->ID);
      if( count( $children ) != 0 ) {

        $args = array(
            'post_type'      => 'page',
            'posts_per_page' => -1,
            'post_parent'    => $post->post_parent,
            'order'          => 'ASC',
            'orderby'        => 'menu_order'
         );


        $parent = new WP_Query( $args );

        if ( $parent->have_posts() ) : ?>

            <?php while ( $parent->have_posts() ) : $parent->the_post();
            $category_title = get_field('category_title');?>

                <a class="btn btn-location" href="<?php the_permalink(); ?>" title="<?php echo ($category_title); ?>">
                  <?php echo ($category_title); ?>
                </a>

            <?php endwhile; ?>

        <?php endif; wp_reset_postdata();

      }

      $parent = get_post_ancestors($post->ID);
      if( count( $children ) <= 0  && empty($parent[1]) ) {

        $args = array(
            'post_type'      => 'page',
            'posts_per_page' => -1,
            'post_parent'    => $post->post_parent,
            'order'          => 'ASC',
            'orderby'        => 'menu_order'
         );


        $parent = new WP_Query( $args );

        if ( $parent->have_posts() ) : ?>

            <?php while ( $parent->have_posts() ) : $parent->the_post();
            $category_title = get_field('category_title');?>

              <a class="btn btn-location" href="<?php the_permalink(); ?>" title="<?php echo ($category_title); ?>">
                <?php echo ($category_title); ?>
              </a>

            <?php endwhile; ?>

        <?php endif; wp_reset_postdata();


      } elseif ( count( $children ) <= 0  && !empty($parent[1]) )  {
        $grandparentPage = wp_get_post_parent_id($post->post_parent);
        $args = array(
            'post_type'      => 'page',
            'posts_per_page' => -1,
            'post_parent'    => $grandparentPage,
            'order'          => 'ASC',
            'orderby'        => 'menu_order'
         );


        $parent = new WP_Query( $args );

        if ( $parent->have_posts() ) : ?>

            <?php while ( $parent->have_posts() ) : $parent->the_post();
            $category_title = get_field('category_title');?>

              <a class="btn btn-location" href="<?php the_permalink(); ?>" title="<?php echo ($category_title); ?>">
                <?php echo ($category_title); ?>
              </a>

            <?php endwhile; ?>

        <?php endif; wp_reset_postdata();

      }

      } else {

      $args = array(
          'post_type'      => 'page',
          'posts_per_page' => -1,
          'post_parent'    => $post->ID,
          'order'          => 'ASC',
          'orderby'        => 'menu_order'
       );


      $parent = new WP_Query( $args );

      if ( $parent->have_posts() ) : ?>

          <?php while ( $parent->have_posts() ) : $parent->the_post();
          $category_title = get_field('category_title');?>

            <a class="btn btn-location" href="<?php the_permalink(); ?>" title="<?php echo ($category_title); ?>">
              <?php echo ($category_title); ?>
            </a>

          <?php endwhile; ?>

      <?php endif; wp_reset_postdata();

      }

      ?>

      </div>
