指定したカテゴリIDのカテゴリ名を出力する
<?php
function get_category_name_by_id($cat_ID) {
$cat_ID = (int) $cat_ID;
$category = &get_category($cat_ID);
echo $category->cat_name;
}
?>
親カテゴリをもつ場合(子カテゴリの場合)のみ、カテゴリ名を出力する
<?php
function child_category_name() {
$cat = get_the_category();
$cat = $cat[0];
$parent_cat = $cat->category_parent;
if ($parent_cat != 0) {
echo $cat->cat_name;
}
}
?>
親カテゴリ名を出力する
<?php
function parent_category_name() {
$cat = get_the_category();
$id = $cat[0];
$parent = &get_category($id);
$name = $parent->cat_name;
if ( $parent->category_parent ) {
$parent = &get_category($parent->category_parent);
$name = $parent->cat_name;
}
echo $name;
}
?>
現カテゴリの投稿一覧を出力する
<?php
$myargs = 'numberposts=0&orderby=ID&order=asc&category='.$cat;
global $post;
$myposts = get_posts($myargs);
foreach($myposts as $post) :
echo '<li><a href="';
echo the_permalink();
echo '">';
echo the_title();
echo '</a></li>';
endforeach;
?>
指定したカテゴリIDの投稿一覧を出力する
<?php
function get_posts_by_category_id($cat_ID) {
$cat_ID = (string) $cat_ID;
$myargs = 'numberposts=0&orderby=ID&order=asc&category='.$cat_ID;
global $post;
$myposts = get_posts($myargs);
foreach($myposts as $post) :
echo '<li><a href="';
echo the_permalink();
echo '">';
echo the_title();
echo '</a></li>';
endforeach;
}
?>