Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My wordpress video blog have several categories. Lets say the category can belong to several genres e.g actions,horror,mystery and another category belong to actions,horror,romance

How do I group categories into genres? Cause in the most basic structure, we group posts into category. Now i want group categories into genres

Then be able to display the category in the genre by a url such as http://www.domain.com/genre/actions and it will list all categories in it. Here's an example site that has this feature http://www.anime44.com/anime-genre/Fantasy

share|improve this question
    
Hi @ruriko Can you provide more examples of your categories to help us understand what you're trying to achieve? You're talking about having a taxonomy of a taxonomy which would be the wrong approach in WP. –  sanchothefat Jun 6 '13 at 9:01
    
You know how tags only work on posts? well I want to do the same but with categories only. Let's just say categories will act as TV series and I want to group those TV series by genres. Let's say I click on genre romance it should list all the categories that is romance –  Ruriko Jun 6 '13 at 10:33
    
WordPress only lists posts based on taxonomy at a certain URL so you'd need to look into something like the Posts 2 Posts plugin so you could have a post for your series then relate the episode posts to that. Are you a developer? –  sanchothefat Jun 6 '13 at 10:55
    
The easy solution would be to use categories hierarchy, genres would be level 1 categories, your categories would be 2nd level. This means you have to duplicate categories, so you can't search just "fantasy" cause you would have more of those "fantasy" categories under different genres. But if you want to search just for genres it definitely fits. –  ggg Jun 6 '13 at 11:21
    
@ggg is there a way to avoid duplicate categories? –  Ruriko Jun 6 '13 at 11:39

2 Answers 2

up vote 3 down vote accepted
+50

It sounds to me like genre should be its own taxonomy. Then posts get assigned to both categories and genres. If you do it this way, here's what you need to do to get everything working:

First, register the taxonomy genre.

Next, assuming you're using the core posts and categories, go to Settings → Permalinks. Set your permalinks to be /genre/%genre%/%category%/%postname%'/ and your category base to be /genre/all/.

Lastly, you'll want to add a filter to post_link so the auto-generated links don't contain the literal string "%genre%":

function wpse_101766_post_type_link( $permalink, $post ) {
    if ( 'post' == $post->post_type ) {
        $genre = 'all';
        if ( false !== strpos( $permalink, '%genre%' ) ) {
            $first_genre = wp_get_object_terms( array( $post->ID ), 'genre', array( 'orderby' => 'term_id', 'count' => 1 ) );
            if ( ! is_wp_error( $first_genre ) && !empty( $first_genre ) ) {
                $first_genre = array_shift( $first_genre );
                $genre = $first_genre->slug;
            }
        }
        return str_replace( '%genre%', $genre, $permalink );
    }

    return $permalink;
}
add_filter( 'post_link', 'wpse_101766_post_type_link', 10, 2 );

As always when making rewrite changes, be sure to flush your rewrite rules. You did this when you saved your changes on the Permalinks page, but if you made changes after that, go back and click "Save Changes" again.

Now, the URLS look like this: (using the genre 'action', category 'fantasy', and post 'lord-of-the-rings':

  • /genre/action/
  • /genre/action/fantasy/
  • /genre/action/fantasy/lord-of-the-rings/

You can also go to:

  • /genre/all/fantasy/ to list all posts in the category fantasy across any genre
  • /genre/action/fantasy,anime/ to list all posts in the genre "action" and in either categories "fantasy" or "anime"
  • /genre/action/fantasy+anime/ to list all posts in the genre "action" and in both categories "fantasy" or "anime"
  • /genre/action,adventure/fantasy+anime/ to list all posts in the genre "action" or the genre "adventure" and in both categories "fantasy" or "anime"

... and so on. It gives you a lot of flexibility!

To list categories by genre, create a taxonomy-genre.php file in your theme. Here's a function to get all categories "within" a genre, just call it like so:

$category_ids = get_terms_by_overlap( array(
    'term_ids' => get_current_object_id(),
    'get_taxonomy' => 'category',
    'through_taxonomy' => 'genre'
) );

You can then loop over this however you'd like. Here's a basic example:

<ul>
    <?php foreach ( $category_ids as $category_id ) : ?>
    <li><a href="<?php echo get_category_link( $category_id ) ?>"><?php echo get_the_category_by_ID( $category_id ) ?></a></li>
    <?php endforeach ?>
</ul>
share|improve this answer
    
If I view /genre/action/ I want it to list the categories and not the posts because categories will act as TV Series and posts act as Episodes. How can I do this? –  Ruriko Jun 7 '13 at 7:08
    
Very easy, just add a taxonomy-genre.php file in your theme and use that to list the categories. You'll probably have to use some custom SQL to get the categories that are attached to posts in the requested genre. It wouldn't be difficult to do, and definitely the path of least resistance for solving this problem. –  Matthew Boynes Jun 7 '13 at 13:41
    
@Ruriko I added a way to list categories by genre for you. Enjoy! –  Matthew Boynes Jun 7 '13 at 21:52

I would suggest using Scribu's Posts 2 Posts plugin.

It allows you to map posts to other posts as if they were a taxonomy like categories. The reason you would do it this way is that your categories for TV series etc... wouldn't have the kind of rich data you want attached to them and it'd be difficult to assign them to multiple genres otherwise. Plus as @ggg said you can't have multiple category parents either.

http://wordpress.org/plugins/posts-to-posts/

Using this plugin you would create hub posts for your TV series etc... Categorise those according to your genre categories (or you could create a genre taxonomy using a plugin like http://wordpress.org/plugins/types/). After that you could create posts for the individual episodes and connect them to your series post.

The posts 2 posts documentation is here:

https://github.com/scribu/wp-posts-to-posts/wiki

Once you have created connections you can use shortcodes in your TV series posts to list all connected posts (eg. episodes).

https://github.com/scribu/wp-posts-to-posts/wiki/Shortcodes

It's difficult to suggest a different approach if you're not familiar with developing WP sites. Hope it helps get you started :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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