0

I’m working on a large site in which I need to create various lists of different pages in the sidebar. I registered a custom taxonomy using the Pods plugin called “Page Categories” (page_category), and have successfully generated the lists of pages required using WP_Query. So far, so good.

Where I’m stuck: Each of these lists is contained in a template part that needs to be conditionally included in sidebar.php based upon the value of the current Page Category.

What I have that isn’t working:

    <?php
      $display = wp_get_post_terms($post->ID, 'page_category', 
      array('fields'=>'slug'));

      switch($display){
         case 'dining-area-1':
           get_template_part('sidebar', 'dining-one');
           break;
         case 'dining-area-2':
           get_template_part('sidebar', 'dining-two');
           break;
       }
     ?>

The result of this is that the first case takes effect on every page, regardless of whether or not the page is associated with that category.

Any ideas?

  • wp_get_post_terms returns an array of objects, not a string – Milo Dec 11 '14 at 17:06
  • @Milo Okay. wp_get_post_terms produces the same result, so obviously it's returning a string, either. I guess I'm confused as to why I can't simply use get_the_terms and check against the results. – DavidBrown Dec 11 '14 at 19:06
  • Ah, hang on...think I've found the answer here: wordpress.stackexchange.com/questions/83922/… – DavidBrown Dec 11 '14 at 19:58
0

@Milo was absolutely correct...the issue was that I wasn't getting a string to compare. The solution actually came from here.

What this ultimately looked like in my scenario:

    <?php
        if(has_term('dining-1', 'page_category', $post->ID)){
          get_template_part('sidebar', 'dining-one');
        }
        elseif(has_term('dining-2', 'page_category', $post->ID)){
          //etc.
        }
    ?>

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.