Display and register custom categories for custom post types

Ok here we go. I have 2 different custom post types. The first is called 'portfolio', the second one 'games'.

All the game reviews I write are 'games' posts. To have a better overview I created custom categories like reviews, playstation xbox etc. Unfortunately everytime I would like to display the posts of one categorie (http://www.zock-around-the-clock.com/games_category/testcategory) I just get an 404 Error Page.

I dont really use the 'portfolio' posts but with them the arrengement into categories would work just perfectly. (http://www.zock-around-the-clock.com/portfolio-view/testcategory).

I would like to ask you what is the difference between these two types and why is it working with the portfolio posts but not with the games posts.

I also tried adding normal categories into the games posts by adding 'taxonomies' = array('category'), but unfortunately it is still not possible to display the games posts of a single category.

Here's the single-portfolio.php

?php get_header(); ?

?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?
?php
  $custom = get_post_custom($post-ID);
  $lightbox = $custom["lightbox-url"][0];
?
div id="post-?php the_ID(); ?" ?php post_class('post'); ?
  article class="single-post"
    header
      h1 class="entry-title"?php the_title(); ?/h1
    /header
    div class="post-content wrapper"
   ?php if(has_post_thumbnail()) {
                echo 'div class="featured-thumbnail no-hover"div class="img-wrap"'; the_post_thumbnail(''); echo '/div/div';
                }
            ?
      ?php the_content(); ?
      ?php wp_link_pages('before=div class="pagination"after=/div'); ?
    /div!--.post-content--
  /article
/div!-- #post-## --

?php comments_template( '', true ); ?

and the init. for the portfolio posts:

function my_post_type_portfolio() {
register_post_type( 'portfolio',
            array( 
            'label' = __('Portfolio'), 
            'singular_label' = __('Porfolio Item', 'theme1575'),
            '_builtin' = false,
            'public' = true, 
            'show_ui' = true,
            'show_in_nav_menus' = true,
            'hierarchical' = true,
            'capability_type' = 'page',
            'menu_icon' = get_template_directory_uri() . '/includes/images/icon_portfolio.png',
            'rewrite' = array(
                'slug' = 'portfolio-view',
                'with_front' = FALSE,
            ),
            'supports' = array(
                    'title',
                    'editor',
                    'thumbnail',
                    'excerpt',
                    'custom-fields',
                    'comments')
                ) 
            );
register_taxonomy('portfolio_category', 'portfolio', array('hierarchical' = true, 'label' = 'Portfolio Categories', 'singular_name' = 'Category', "rewrite" = true, "query_var" = true));}add_action('init', 'my_post_type_portfolio');

Here's the single-games.php

?php get_header(); ?div id="content" class="grid_8 ?php if (of_get_option('blog_sidebar_pos') == "right" ) {echo "alpha";} else {echo "omega";} ? ?php echo of_get_option('blog_sidebar_pos') ?"
?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?
div id="post-?php the_ID(); ?" ?php post_class('post'); ?
  article class="single-post"
    div class="post-header"
        h2 class="entry-title"a href="?php the_permalink() ?" title="?php the_title(); ?" rel="bookmark"?php the_title(); ?/a/h2
        ?php $post_meta = of_get_option('post_meta'); ?
        ?php if ($post_meta=='true' || $post_meta=='') { ?
            div class="post-meta"
                ?php _e('Geposted am', 'theme1575'); ? span class="date updated"?php the_time('j. F, Y'); ?/span i?php _e('by', 'theme1575'); ? span class="vcard author" span class="fn"?php the_author_posts_link() ?/span /span/i
            /div!--.post-meta--
        ?php } ?      
    /div
    div class="post-content wrapper"
    ?php if(has_post_thumbnail()) {
                echo 'div class="featured-thumbnail no-hover"div class="img-wrap"'; the_post_thumbnail(''); echo '/div/div';
                }
            ?
      ?php the_content(); ??php wp_link_pages('before=div class="pagination"after=/div'); ?
    /div!--.post-content--
  /article



        ?php /* If a user fills out their bio info, it's included here */ ?
  div id="post-author"
    h3?php _e('Written by', 'theme1575'); ? ?php the_author_posts_link() ?/h3
    p class="gravatar"?php if(function_exists('get_avatar')) { echo get_avatar( get_the_author_email(), '80' ); /* This avatar is the user's gravatar (http://gravatar.com) based on their administrative email address */  } ?/p
    div id="author-description"
      ?php the_author_meta('description') ? 
      div id="author-link"
        p?php _e('View all posts by:', 'theme1575'); ? ?php the_author_posts_link() ?/p
      /div!--#author-link--
    /div!--#author-description --
  /div!--#post-author--

/div!-- #post-## --


nav class="oldernewer"
  div class="older"
    ?php previous_post_link('%link', __('laquo; Previous post', 'theme1575')) ?
  /div!--.older--
  div class="newer"
    ?php next_post_link('%link', __('Next Post raquo;', 'theme1575')) ?
  /div!--.newer--
/nav!--.oldernewer--

and the init. for the games posts:

function my_post_type_games() {
register_post_type( 'games',
            array( 
            'label' = __('Games'), 
            'taxonomies' = array('category'),
            'public' = true, 
            'query_var'= true, 
            'publicly_queryable'= true,
            'show_ui' = true,
            'show_in_nav_menus' = false,
            'menu_position' = 5,
            'rewrite' = array(
                'slug' = 'games-view',
                'with_front' = true,
                'hierarchical' = true,
            ),
            'has_archive' = true,
            'supports' = array(
                    'title',
                    'author',
                    'thumbnail',
                    'revisions',
                    'comments',
                    'editor',
                    'excerpt')
                ) 
            ); register_taxonomy('games_category', 'games', array('hierarchical' = true, 'label' = 'Games Categories', 'singular_name' = 'Category', "rewrite" = true, "query_var" = true)); } add_action('init', 'my_post_type_games');?

Topic custom-taxonomy custom-post-types Wordpress

Category Web


you just need to create taxonomy-testcategory.php in your theme folder and reset your permalinks


Put hierarchical=false (in the post type) if you have a wordpress > 3.6. I think there is a bug that prevents the taxonomies to work correct, I've used that trick to my cpt / custom taxonomy and it works ok.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.