wordpressシングルページにカテゴリー別の情報を掲載する

Filed under: wordpress — kdcs @ 16年4月30日 土曜日

シングルページに記事以外の「カテゴリー分類した情報」を掲載する手段

for文 複数カテゴリーのIDを指定、さらに並び順もIDの並び順で指定できる

<?php
$categorys = array(4,6,5);
for ($i=0; $i<count($categorys); $i++) :
?>
<h3><?php echo esc_html(get_catname($categorys[$i])); ?></h3>
<ul>
<?php
query_posts('showposts=5&cat='.$categorys[$i]);
if (have_posts()) : while (have_posts()) : the_post();
?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<li><?php echo esc_html(get_catname($categorys[$i]))."はまだありません。"; ?></li>
</ul>
<?php endif; ?>
<?php endfor; ?>

単一カテゴリーならforeach文でOK

<?php
$categories = get_categories();
foreach($categories as $category) :
?>
<h3><?php echo $category->cat_name; ?></h3>
<ul>
<?php
query_posts('showposts=5&cat='.$category->cat_ID);
if (have_posts()) : while (have_posts()) : the_post();
?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>
</ul>
<?php endforeach; ?>

さらにカスタム投稿(こちらは未検証)
タクソノミーは「faq」

<?php
$categorys = array(9,10);
for ($i=0; $i<count($categorys); $i++) :
$faq_term = get_term($categorys[$i], 'faq');
?>
<h3><?php echo esc_html($faq_term->name); ?></h3>
<ul>
<?php
query_posts('showposts=5&faq='.$faq_term->slug);
if (have_posts()) : while (have_posts()) : the_post();
?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<li><?php echo esc_html($faq_term->name)."はまだありません。"; ?></li>
</ul>
<?php endif; ?>
<?php endfor; ?>

サイト内検索

カテゴリー

最近の投稿

« |wordpressシングルページにカテゴリー別の情報を掲載する| »
↑上に戻る