2

I have a custom post type and I'm trying to call up different variations of the navigation based on the custom taxonomy slug. I've done this fairly easily with normal posts, like so:

<?php 
    if ( is_category( 'mixers' )) {
        include (TEMPLATEPATH.'/nav-mixers.php');
    } elseif ( is_category( 'monitors' )) {
        include (TEMPLATEPATH.'/nav-monitors.php' );
    } elseif ( is_category( 'speakers' )) {
        include (TEMPLATEPATH.'nav-speakers.php');
    }
?>

however this is proving challenging for a custom post type. I feel like I'm close but I need some help now. Here's where I currently am.

<?php
    $terms = get_the_terms( $post->id, 'prodcat' ); // get an array of all the terms as objects.
    $terms_slugs = array();
        foreach( $terms as $term ) {
            $terms_slugs[] = $term->slug; // save the slugs in an array
        }
    if( $terms ) :
       get_template_part( 'nav', slug );
    else :
       get_template_part( 'nav', 'home' );
    endif;
?>

Any help is much appreciated!

4
  • 1
    You know that your above code doesn't work all, don't you? Should already throw a bunch of errors and stop your page rendering.
    – kaiser
    Commented Jul 31, 2013 at 18:55
  • 1
    The point of this community is to help fix broken code. So yes, I know it doesn't work :) Commented Jul 31, 2013 at 19:22
  • 1
    Kristina, I guess you grabbed that copypasta somewhere? :) As get_template_part( 'nav', slug ); is already missing the $ for the variable. Would you mind adding a link to the original source here in the comments or as edit to the question?
    – kaiser
    Commented Jul 31, 2013 at 19:36
  • 1
    I've been taking bits and pieces of code from people trying to do similar things. I cobbled this together in a flailing attempt to figure out how the custom taxonomy syntax works. Commented Jul 31, 2013 at 20:20

3 Answers 3

2

To loop through all the slugs of a term list, simply call get_the_terms() and pull only the slugs:

$slugs = wp_list_pluck( get_the_terms( get_the_ID(), 'prodcat' ), 'slug' );

Then you need to check if you got any results:

if ( ! empty( $slugs ) )

The problem I then see arising is that you got a bunch of slugs in return (unless you restricted the admin meta box to allowing only a single term).

You'd then have to decide on some custom criteria which nav menu you want to have and pull that from the list of $slugs:

// Decide which slug fits and then determine the key:
$key = 0;
get_template_part( 'nav', $slugs[ $key ] );
2

The "custom taxonomy slug", in your case, would be prodcat but based on your code I assume that you mean the individual term slugs.

Now, get_the_terms() will return all terms assigned to the post but you can only load one template so you will have to determine which term slug to use if there are more than one. I don't know how you intend to decide that but this will load a template based on one of the slugs, anyway.

$terms = get_the_terms( $post->id, 'post_tag' ); // get an array of all the terms as objects.
$terms_slugs = array();
foreach( $terms as $term ) {
    $terms_slugs[] = $term->slug; // save the slugs in an array
}

if( !empty($terms_slugs) ) :
  get_template_part( 'nav', array_pop($terms_slugs) );
else :
  get_template_part( 'nav', 'home' );
endif;

But I am not even sure you need the foreach at all:

// get an array of all the terms as objects.
$terms = get_the_terms( $post->id, 'post_tag' );
if ( ! empty( $terms ) ) :
    $terms = array_pop( $terms );
    get_template_part( 'nav', $terms->slug );
else :
    get_template_part( 'nav', 'home' );
endif;
9
  • 1
    Similar solution... +1 ... Still wonder which criteria would decide on that. So far the OPs idea itself seems pretty random to me.
    – kaiser
    Commented Jul 31, 2013 at 19:00
  • 1
    @kaiser : yes, I agree. I am not sure about a number of things on this question.
    – s_ha_dum
    Commented Jul 31, 2013 at 19:07
  • "High reputation comes with high number of open questions" ;)
    – kaiser
    Commented Jul 31, 2013 at 19:14
  • hmm.... this still isn't working for me. i can't figure out why Commented Jul 31, 2013 at 19:26
  • 1
    Without more information, neither can I, but my first guess is that your template names are wrong.
    – s_ha_dum
    Commented Jul 31, 2013 at 19:35
2

Well, it took a 24 hour work session to get me just loopy enough to figure it out. I had to pass both post id AND taxonomy name. Everything I had tried until this did either one or the other. facepalm

<?php

    $terms = get_the_terms( $post->id, 'prodcat', array( 'parent' => 0 ) ); 
    $terms_slugs = array();
    foreach( $terms as $term ) {
        $terms_slugs[] = $term->slug; 
    }

    if( !empty($terms_slugs) ) :
      get_template_part( 'nav', array_pop($terms_slugs) );
    else :
      get_template_part( 'nav', 'home' );
    endif;
?>

WOO!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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