【wordpress】投稿タグで記事を分類する

投稿した記事はカテゴリーや投稿フォーマットだけでなく、投稿タグで分類することもできる。
記事ごとに投稿タグを指定して、タグページで同じ投稿タグを持つ記事を閲覧できる様にする。

content.single.php

<p class="postinfo">
<?php if(has_tag()): ?>
<?php the_tags('TAGS: ','・'); ?>
 |
<?php endif; ?>
<?php echo get_the_date(); ?>
</p>

投稿タグを表示する。

tag.php

<?php get_header(); ?>

<!-- パンくずリスト -->
<div id="breadcrumb">
<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
	<a href="<?php echo home_url(); ?>" itemprop="url">
		<span itemprop="title">トップ</span>
	</a> &rsaquo; 
</div>

<div><?php single_cat_title(); ?></div>
</div>

<!-- コンテンツ -->
<div id="content">
<p class="title"><?php single_cat_title(); ?></p>

<?php query_posts('posts_per_page=-1&order=ASC&' . $query_string); ?>
<?php if(have_posts()): while(have_posts()): 
the_post(); ?>
	<?php get_template_part('content','excerpt'); ?>
<?php endwhile; endif; ?>

<?php get_template_part('pagenation'); ?>
</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

◼︎カスタム投稿タイプを利用してニュース記事を投稿する
archive.php

<?php get_header(); ?>

<!-- パンくずリスト -->
<div id="breadcrumb">
<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
	<a href="<?php echo home_url(); ?>" itemprop="url">
		<span itemprop="title">トップ</span>
	</a> &rsaquo; 
</div>

<div><?php $myposttype = get_post_type_object($post_type); ?>
<?php echo $myposttype->label; ?></div>
</div>

<!-- コンテンツ -->
<div id="content">
<p class="title"><?php echo $myposttype->label; ?></p>

<?php query_posts('posts_per_page=5&order=DESC&' . $query_string); ?>
<?php if(have_posts()): while(have_posts()): 
the_post(); ?>
	<?php get_template_part('content'); ?>
<?php endwhile; endif; ?>

<?php get_template_part('pagenation'); ?>
</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

single.-news.php

<?php get_header(); ?>

<!-- パンくずリスト -->
<div id="breadcrumb">
<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
	<a href="<?php echo home_url(); ?>" itemprop="url">
		<span itemprop="title">トップ</span>
	</a> &rsaquo; 
</div>

<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
	<a href="<?php echo get_post_type_archive_link($post_type); ?>" itemprop="url">
		<span itemprop="title">
		<?php $myposttype = get_post_type_object($post_type); ?>
		<?php echo $myposttype->label; ?>
		</span>
	</a> &rsaquo; 
</div>

<div><?php the_title(); ?></div>
</div>

<!-- コンテンツ -->
<div id="content">

<?php if(have_posts()): while(have_posts()): 
the_post(); ?>
	<?php get_template_part('content','single'); ?>

	<p class="pagenation">
	<span class="oldpage"><?php previous_post_link(); ?></span>
	<span class="newpage"><?php next_post_link(); ?> </span>
	</p>
<?php endwhile; endif; ?>
</div>

<!-- サイドバー -->
<div id="sidebar">
<ul>
<li class="widget"><h2>最近のニュース</h2>
	<ul>
	<?php query_posts('post_type=news&posts_per_page=5'); ?>
	<?php if(have_posts()): while(have_posts()): 
	the_post(); ?>
		<?php get_template_part('content','title'); ?>
	<?php endwhile; endif; ?>
	</ul>
</li>
</ul>
</div>

<?php get_footer(); ?>

home.php

<p class="title">NEWS</p>

<ul class="newslist">
<?php query_posts('post_type=news&posts_per_page=2'); ?>
<?php if(have_posts()): while(have_posts()): 
the_post(); ?>
	<?php get_template_part('content','title'); ?>
<?php endwhile; endif; ?>
</ul>

posts_per_page=2は表示する記事の数。


functions.php

//ニュース記事の投稿タイプ
register_post_type(
	'news', 
	array(
		'label' => 'ニュース',
		'hierarchical' => false,
		'public' => true,
		'has_archive' => true,
		'supports' => array(
			'title',
			'editor'
		)
	)
);