0

I have a page divided in two bars.

  • The left bar is a side menu, which shows contents for a specific country (ie: if I am in the Mexico page, there are options listing Cancun, Acapulco, Mexico City... if I am in Italy page, there are options listing Rome, Milan, Florence and so on);
  • The right bar is the full content, that display the pack information of the destination, so if I am in the Italy page and click on Rome, the user will see in this right bar all the information regarding prices, flights, sightseeing and so on;

I have to match these two bars each other. When I am checking the content of the destination in the right bar, the left bar should match the correct menu country.

The way I analyzed it was applying taxonomies for the menu and for the pack information. So, when I am making a page for Rome, I apply the "Italy" taxonomy, or if I am making a page for Cancun, I apply the "Mexico" taxonomy and so on for many other destinations I have.

That is the way I am querying the left bar/side menu (content-sidemenu.php)

<?php
  $args = array(
      'post_type'     => 'side_menu',
      'posts_per_page'=> 1,
      'tax_query'     => array(
        'relation'    => 'AND',
        array(
          'taxonomy' => 'countries',
          'field'    => 'slug',
          'terms'    => array( **??????** )
          )
        )
  );
  $the_menu = new WP_Query( $args );
  if ( have_posts() ) : while ( $the_menu->have_posts() ) : $the_menu->the_post(); ?>
?>

The right bar is slightly similar (just the post_type has different values depending on the page), but the tax_query is the same. So, how could I make them match??

Thank you!

2
  • it's not really clear what your structure is. you mention multiple taxonomies, but not what those taxonomies are. perhaps you mean multiple terms within a single taxonomy?
    – Milo
    Jul 15, 2014 at 15:50
  • Yeah, I have 32 countries registered in a taxonomy called "countries". My site structure contains two bars (left and right, as I mentioned). If the right side shows content about "Italy", so the left side should display the menu for Italy. In my CPT called "side_menu" I check the taxonomy for Italy. And for my CPT called "pack" I check the taxonomy for Italy. So I need to match the "side_menu" terms (italy, mexico, brazil, usa, canada...) with the "pack" terms when both are displayed in the bars! Jul 15, 2014 at 16:23

1 Answer 1

0

You'll want to have a look at get_the_terms().

<?php
  // You need to define the post id from the post you are outputting in the right bar so that the left bar knows how to match up the terms
  $country = get_the_terms( $post->id, 'countries' )

  $args = array(
      'post_type'     => 'side_menu',
      'posts_per_page'=> 1,
      'tax_query'     => array(
        // 'relation'    => 'AND', /* I don't think you need this unless it has another purpose */
        array(
          'taxonomy' => 'countries',
          // This only works since you are selecting just one term per post
          // Works by retrieving the first and only term id from the post in the right bar
          'terms'    => array_shift(array_keys($country))
          )
        )
  );
  $the_menu = new WP_Query( $args );
  if ( have_posts() ) : while ( $the_menu->have_posts() ) : $the_menu->the_post(); ?>
?>

Your Answer

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

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