投稿の子カテゴリに属するタイトル、概要を表示

WordPressでは、投稿には、一般的にカテゴリリストが表示されますが、カテゴリリストではなく、投稿に子カテゴリが存在する場合に、その、子カテゴリに属する投稿一覧を表示する方法を考えてみました。

以下のコードは、カテゴリアーカイブを表示したときに、投稿に子カテゴリが存在する場合、子カテゴリに属する投稿のタイトルと概要を表示します。

この作業には http://wordpress.org/plugins/category-post-shortcode/(category post shortcode プラグインが必要です)

<article>
do stuff......
		<?php 
		if( is_category() ) {
		
			$category_parents= wp_get_post_categories( $post->ID );
			
			$child_categories= '';

			foreach( $category_parents as $cat_parent ) {
			
					$child_categories .= get_category_children($cat_parent);
			}
			
			if( !empty( $child_categories ) ) { 
			
				$child_categories= str_replace( '/',',', $child_categories );
				$child_categories= trim( $child_categories,',');
				
				//wp_list_categories('orderby=name&amp;title_li=&amp;include='.$child_categories ); 
				
				echo do_shortcode('[cat totalposts="3" category="'.$child_categories.'" thumbnail="false" excerpt="true"]');
			}

			
			
		}
		?>
		
	</article>

[emulsion_relate_posts]