1

I have a custom post type called freebies. This custom post type has its own taxonomy with the "normal" categories from wordpress, (which are child-categories from freebies) and own custom taxonomy used as tags (called freebie-tags)

I have managed to have own templates for the freebies: archive-freebies.php and the posts from child categories of freebies are displayed in category-freebies.php. The tags from freebies are displayed in the template taxonomy-freebie-tags.php

Now when the visitor clicks on a category in freebies, he sees the posts in this category, and when clicking on a tag in freebies, he sees those posts with the tag.

I'd like to combine these two, so that when the visitor is in a child-category, and clicks on a tag (they are displayed on the sidebar as a cloud) he will see a template which shows only those posts which are a) in the child-category and b) have the specific tag.

The other way round (also when viewing posts with a certain tag and clicks on category) is not neccessary.

So this question is

  1. How to tell wordpress that it has to use a another template like category-freebies-taxonomy-freebies-tag.php

  2. how to build the links for the cloud that they show posts only from this specific child category where the user is now, and tagged with this tag?

Sorry if here is no efford from my side, but I had so much difficulties with showing the appropriate templates for so far, that I have no idea where to go further with this. I searched here and google also, but did not found anything, since this is not related to normal post, rather to custom post type...

Thx!

2 Answers 2

0

Maybe change the permalinks for the tags in the sidebar to point to the category page they're already viewing then pass the tag as a url parameter. If the parameter is set use pre_get_posts to merge the tag into the main query so it filters by both the category and tag.

Something along these lines

function filter_by_tag_too( $query ) {
  if ( isset( $_GET['tag'] ) ) {
    $query->set( 'tag', $_GET['tag'] );
  }
}
add_action( 'pre_get_posts', 'filter_by_tag_too' );
3
  • Thank you very much, this sound really logical! I think I add a filter/query so that the category pages show also only the tags related to that category (they are already related to the freebies taxonomy) I will try this with your answer
    – Owl
    Jun 7, 2014 at 15:44
  • Did this work out for you?
    – Joey Yax
    Jun 11, 2014 at 22:09
  • Sorry Joey I have not time to try at the moment, but I will let you know as soon as I could try it!
    – Owl
    Jun 13, 2014 at 6:08
0

Concerning the second part of your question (building the links): I had a similar problem with combining tags and categories for a menu on a Woocommerce page. I ended up utilizing wp_list_categories and made use of the output filter of that function.

Filtering the URLs with that filter (credit: https://stackoverflow.com/questions/5809774/manipulate-a-url-string-by-adding-get-parameters) I picked up the taxonomy that was used on that particular archive page and the subsequent $_GET parameters, that were the passed down in the menu URLs. In the end it looked like this and could e.g. be placed into your function.php:

function add_product_tag_too( $output, $args ) {

  // if on a product tag archive or with active _GET
  if ( is_tax('product_tag') || $_GET['product_tag'] != "" ) {

    function AppendCampaignToString($string) {
      $regex = '#(<a href=")([^"]*)("[^>]*?>)#i';
      return preg_replace_callback($regex, '_appendCampaignToString', $string);
    }
    function _AppendCampaignToString($match) {
      if ( is_tax('product_tag') )
        $tag_slug = get_term(get_queried_object()->term_id)->slug;
      else
        $tag_slug = $_GET['product_tag'];

      $url = $match[2];
      if (strpos($url, '?') === false) {
          $url .= '?';
      }
      $url .= '&product_tag='.$tag_slug;
      return $match[1].$url.$match[3];
    }

    $output = AppendCampaignToString($output);
  }

  return $output;
}
add_filter( 'wp_list_categories', 'add_product_tag_too', 15, 2 );

Your Answer

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

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