最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

groupby - Grouping categories by genre

programmeradmin3浏览0评论

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 and it will list all categories in it. Here's an example site that has this feature

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/genre/actions and it will list all categories in it. Here's an example site that has this feature http://www.anime44/anime-genre/Fantasy

Share Improve this question edited Jun 6, 2013 at 10:27 Ruriko asked Jun 3, 2013 at 23:42 RurikoRuriko 5433 gold badges19 silver badges37 bronze badges 7
  • 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 Commented Jun 6, 2013 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 Commented Jun 6, 2013 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 Commented Jun 6, 2013 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 Commented Jun 6, 2013 at 11:21
  • @ggg is there a way to avoid duplicate categories? – Ruriko Commented Jun 6, 2013 at 11:39
 |  Show 2 more comments

2 Answers 2

Reset to default 3 +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>

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/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/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/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/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 :)

发布评论

评论列表(0)

  1. 暂无评论