1

Right now I am outputting the related categories to the current displayed post type as shown below. I am using wp_list_categories to list out the categories and it works just fine. The only issue is that this is being displayed on archive.php and not a single post, so the Taxonomies that are being listed link to their main taxonomy archive.

I would like to link to the taxonomy archive for the current post type ($type) as well as include the query to display that specific taxonomy. Similar to linking to http://example.com?post_type=foo&custom_tax=bar

<? $type = get_post_type(); ?>
                <?php 

                $customPostTaxonomies = get_object_taxonomies($type);

                if(count($customPostTaxonomies) > 0)
                {
                echo '<h3>Browse By:</h3>';
                     foreach($customPostTaxonomies as $tax)
                     {
                         $args = array(
                              'orderby' => 'name',
                              'show_count' => 0,
                              'pad_counts' => 0,
                              'hierarchical' => 1,
                              'taxonomy' => $tax,
                              'title_li' => ''
                            );
                         $woah = get_taxonomy($tax);
                        echo '<ul>';
                        //var_dump($tax);
                        //var_dump($woah);
                        //print_r($woah);
                        echo '<h4>'.$woah->labels->name.'</h4>';
                        echo wp_list_categories( $args );
                        echo '</ul>';
                     }
                }

                ?>`
4
  • 1
    I think instead of wp_list_categories() you want Function Reference/get the term list « WordPress Codex
    – GhostToast
    Aug 29 '13 at 17:22
  • @GhostToast I cannot understand how what you suggest should help.
    – gmazzap
    Aug 29 '13 at 17:56
  • 1
    I thought wp_list_categories only worked for default category. I was wrong. In that case, I'm not sure why the code above isn't working.
    – GhostToast
    Aug 29 '13 at 17:58
  • The code works. OP want only change term link outputted by wp_list_categories. He says: "it works just fine" (2nd line) and "I would like to link to the taxonomy archive for the current post type as well as include the query to display that specific taxonomy"
    – gmazzap
    Aug 29 '13 at 18:31
3

wp_list_categories uses get_term_link to retrive the terms link.

This function have a filter that you can use to change what is returned. A problem is that you have to pass the current post type to the function that hooks into the filter, but a global variable should works for the scope.

Of course you have to remove filter after all wp_list_categories calls, for not interfere with nex class of get_term_link.

So, in your functions.php put:

function convert_term_link_to_post_type( $termlink, $term, $taxonomy ) {
  global $the_current_type;
  if ( empty($the_current_type) ) return $termlink;
  $link = get_post_type_archive_link( $the_current_type );
  if ( $taxonomy == 'category') $taxonomy = "category_name";
  if ( $link ) return add_query_arg( array($taxonomy => $term->slug), $link );
  return $termlink;
}

Then change the code you posted like so:

$type = get_post_type();
$customPostTaxonomies = get_object_taxonomies($type);
if( count($customPostTaxonomies) > 0) {
  echo '<h3>Browse By:</h3>';
  // set the global variable
  global $the_current_type;
  $the_current_type = $type;
  // add the filter that convert the term link
  add_filter('term_link', 'convert_term_link_to_post_type', 20, 3);
  foreach($customPostTaxonomies as $tax) {
    $woah = get_taxonomy($tax);
    $args = array(
      'orderby' => 'name',
      'show_count' => 0,
      'pad_counts' => 0,
      'hierarchical' => 1,
      'taxonomy' => $tax,
      'title_li' => ''
    );
    echo '<ul>';
    echo '<h4>' . $woah->labels->name . '</h4>';
    echo wp_list_categories( $args );
    echo '</ul>';
  }
  // unset the global variable
  unset($the_current_type);
  // remove the filter to not alter any other 'get_term_link' calls
  remove_filter('term_link', 'convert_term_link_to_post_type', 20, 3);
}
4
  • Thanks! Would you happen to know how to get hide_empty and show_count working? They both work, but it's getting the count for all posts and not the current post type.
    – neoian
    Aug 29 '13 at 20:20
  • I still haven't figured out how to get the proper categories for the current term listed. It shows taxonomy related to the post, but it also shows empty ones which is confusing. I have tried hide_empty but it checks against ALL post types and not just the current one.
    – neoian
    Sep 6 '13 at 15:41
  • The problems is related to how taxonomy works in WP. When wordpress count for terms it does't distinct among post types. If the taxonomy is shared by more types becomes very hard and poor performant differentiate terms count and/or term existence for post types...
    – gmazzap
    Sep 6 '13 at 16:09
  • This helped me a lot! but in my case, i knew the archive and taxonomies i want to change, so i check if the taxonomy is one of the types i want and them change the link Mar 19 '20 at 2:06

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.