WordPress

Wyświetlaj i rejestruj niestandardowe kategorie dla niestandardowych typów postów

  • 25 lutego, 2014
  • 6 min read
Wyświetlaj i rejestruj niestandardowe kategorie dla niestandardowych typów postów


Ok, zaczynamy. Mam 2 różne niestandardowe typy postów. Pierwszy nazywa się „portfolio”, drugi „gry”.

Wszystkie recenzje gier, które piszę, to posty „gry”. Aby mieć lepszy przegląd, stworzyłem niestandardowe kategorie, takie jak recenzje, playstation xbox itp. Niestety za każdym razem, gdy chcę wyświetlić posty z jednej kategorii (po prostu otrzymuję stronę błędu 404.

Naprawdę nie używam postów „portfolio”, ale z nimi podział na kategorie działałby idealnie. (

Chciałbym zapytać, jaka jest różnica między tymi dwoma typami i dlaczego działa z postami w portfolio, ale nie z postami z gier.

Próbowałem też dodawać normalne kategorie do postów z grami, dodając 'taxonomies' => array('category'), ale niestety nadal nie jest możliwe wyświetlanie postów z gier z jednej kategorii.

Oto pojedynczy-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 ); ?>

i init. dla postów w portfolio:

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');

Oto plik 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=" 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 ( 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-->

i init. posty dotyczące gier:

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');?>


Źródło

Warto przeczytać!  Wyświetl niestandardowe pole obrazu w profilu użytkownika