カテゴリを(単一または、複数 カンマ区切り)指定して、カテゴリに属する投稿リンクを表示する。
子カテゴリは、親カテゴリが、チェックされている場合は表示します。
template
my_category_posts_link( array('category'=> 'Uncategorized, news' ) );
functions.php
<?php
function my_category_posts_link( $args ){
$defaults= array( 'posts_per_page'=> -1, 'numberposts'=> -1, 'offset'=> 0, 'category'=> '', 'orderby'=> 'post_date', 'order' => 'DESC', 'include'=> '', 'exclude'=> '', 'meta_key'=> '', 'meta_value'=> '', 'post_type' => 'post', 'post_mime_type'=> '', 'post_parent'=> '', 'post_status'=> 'publish', 'suppress_filters'=> true, 'title'=> __('Category Posts','text_domain'), 'title_before'=> '<h3>', 'title_after'=> '</h3>', 'show_post_count'=> -1,'remove_class'=> false ,'echo'=> true);
$r = wp_parse_args( $args, $defaults );
$post_datas= get_posts( $r );
extract( $r );
$i = 0;
if ( $show_post_count !== -1 ) {
$i_max= $show_post_count;
} else {
$i_max= 1;
}
if ( strpbrk($category ,',' ) ) {
$pick_up_category= split(",",$category) ;
if( $remove_class== false ){//for mb category string
$list_class= implode(' ',$pick_up_category);
}else{
$list_class= '';
}
} else {
$pick_up_category= array( $category );
if ( $remove_class== false ) {//for mb category string
$list_class= $category;
} else {
$list_class= '';
}
}
$title = $title_before . $title . $title_after;
$html = "\n".'<li><a href="%1$s">%2$s</a></li>';
$result = '%1$s<ul class="%2$s">%3$s</ul>';
$group = '';
foreach ( $post_datas as $key=> $data ) {
setup_postdata($data);
if ( has_category( $pick_up_category , $data->ID) and $i < $i_max ) {
$url = get_permalink( $data->ID );
$group .= sprintf( $html, esc_url( $url ) , esc_html( $data->post_title ) );
if ( $show_post_count !== -1 ) {
$i++;
}
}
}
wp_reset_postdata( );
if ( $echo== true ){
printf( $result, $title, $list_class, $group );
} else {
return sprintf( $result, $title, $list_class, $group );
}
}
?>
$args= array( |
|
'posts_per_page'=> -1, |
全て |
'numberposts'=> -1, |
全て |
'offset'=> 0, |
|
'category'=> '', |
|
'orderby'=> 'post_date', |
|
'order' => 'DESC', |
|
'include'=> '', |
|
'exclude'=> '', |
|
'meta_key'=> '', |
|
'meta_value'=> '', |
|
'post_type' => 'post', |
|
'post_mime_type'=> '', |
|
'post_parent'=> '', |
|
'post_status'=> 'publish', |
|
'suppress_filters'=> true, |
|
'title'=> __('Category Posts','text_domain'), |
リストのタイトル |
'title_before'=> '<h3>', |
タイトル開始タグ |
'title_after'=> '</h3>', |
タイトル終了タグ |
'show_post_count'=> -1, |
リンクの表示件数(デフォルト全て) |
'remove_class'=> false , |
クラスを記述しない場合、日本語カテゴリなどで |
'echo'=> true); |
エコーするかリターンするか |