wordpress アーカイブページにカスタム分類のターム一覧を複数ループで表示する 2012年8月30日
Tag: |

アーカイブページにカスタム投稿で作成したカスタム分類の複数タームを
一覧で表示させるいろいろな方法です。

↓こんな感じ

例:
カスタムタイプ名:トピックス topics
カスタム分類名:トピックス一覧 topicslist
ターム名:お知らせ info
     オープンハウス open

1.まずは、カスタム分類を表示させるテンプレートをarchive.phpを使って作成します。archive-topics.php

■パターン1

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array(
	'post_type' => 'topics', 
	'taxonomy' => 'topicslist',
	'term' => 'open',
	'posts_per_page' => 4,
	'paged' => $paged,
	'order' => 'DESC'
	)
);

if (have_posts()) :while (have_posts()) :
	the_post();
?>
<div class="topics-box">
<h1 class="p-title"><?php
echo get_the_term_list( $post->ID, 'topicslist' );
?></h1><br />
<div id="entry-25" class="topics-asset">
			<ul>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
</ul>
</div>
<div class="right-bt"><a href="a"><img src="images/topic_bt01_off.gif" alt="オープンハウス情報一覧" width="100" height="29" /></a></div>
</div>
<?php
endwhile;
endif;
wp_reset_query();
?>
‘post_type’ => ‘topics’, // カスタム投稿タイプ名
‘taxonomy’ => ‘topicslist’, // カスタム分類名
‘term’ => ‘open’, // ターム名
‘posts_per_page’ => 4, // 表示件数

■パターン2

<div class="topics-box">
<?php $tax_posts = get_posts('post_type=topics&taxonomy=topicslist&term=open&posts_per_page=3'); if($tax_posts): ?>
<h1 class="p-title">
<?php
echo get_the_term_list( $post->ID, 'topicslist' );
?><br />
</h1>
<div id="entry-25" class="topics-asset">
	<ul>
		<?php foreach($tax_posts as $tax_post): ?>
		<li><a href="<?php echo get_permalink($tax_post->ID); ?>"><?php echo esc_html($tax_post->post_title); ?></a></li>
		<?php endforeach; ?>
	</ul>
</div>
<?php endif; ?>
</div>

get_posts(‘post_type=topics&taxonomy=open&term=topicslist&posts_per_page=3‘);

■パターン3

<?php
$taxonomy_name = 'topicslist';
$taxonomys = get_terms($taxonomy_name);
if(!is_wp_error($taxonomys) && count($taxonomys)):
	foreach($taxonomys as $taxonomy):
		$tax_posts = get_posts(array('post_type' => get_post_type(), 'taxonomy' => $taxonomy_name, 'term' => $taxonomy->slug ) );
		if($tax_posts):
?>
		<div class="topics-box">
			<h1 class="p-title"><?php echo esc_html($taxonomy->name); ?></span></h1>
<div id="entry-<?php the_ID(); ?>" class="topics-asset">
			<ul>
				<?php foreach($tax_posts as $tax_post): ?>
				<li><a href="<?php echo get_permalink($tax_post->ID); ?>"><?php echo esc_html($tax_post->post_title); ?></a></li>
				<?php endforeach; ?>
			</ul>
</div>

<div class="right-bt"><a href="a"><img src="/wordpress/wp-content/themes/lifecrayon/images/topic_bt01_off.gif" alt="オープンハウス情報一覧" width="100" height="29" /></a></div>

		</div>
<?php
		endif;
	endforeach;
endif;
?>

$taxonomy_name = ‘topicslist’; // カスタム分類名

■パターン4(複数のタームを指定する場合

<?php
    $args=array(
        'tax_query' => array(
            array(
                'taxonomy' => 'topicslist', //タクソノミーを指定
                'field' => 'slug', 
                'terms' => array( 'info','open') //表示したいタームをスラッグで指定
            ),
        ),
        'post_type' => 'topics', //カスタム投稿名
        'posts_per_page'=> 5 //表示件数(-1で全ての記事を表示)
    );
 ?>
<?php query_posts( $args ); ?>
<?php if(have_posts()): ?>
<?php while(have_posts()):the_post(); ?>
 
ページが存在する場合の指定
 
<?php endwhile; else: ?>
 
ページが存在しない場合の指定
 
<?php endif; ?>
<?php wp_reset_query(); ?>

New

Aechive

Tag

css Facebook facebookカスタマイズ Facebookページ font functions.php google NEWマーク RSS single.php webツール WEBデザインツール Wordpress アーカイブ カスタム カスタムフィールド カスタムフィールドテンプレート カスタム投稿 カテゴリー カレンダー サイドバー ターム テキスト テンプレートタグ プラグイン ヘッダ ページ ポップアップ メニュー 並び替え 並べ替え 人気 便利 分岐 印刷 年別アーカイブ 投稿 投稿記事 改行 文字数 文法チェック 最新記事 月別アーカイブ 特定カテゴリー 画像