Tool & Snippet

WEB系/3DCG・映像系に関すること

WPスニペット・現在のカテゴリーから最新記事を表示+サムネ付

仕様:
  • マルチサイトを構築
  • 投稿記事を表示しているページで、その記事のカテゴリ内で最新記事を5件表示。
  • アイキャッチで登録した画像のサムネイルを表示
CSS:
#recentEntries { width:240px; }
.mBtm10 { margin-bottom:1em; }
.fLeft { float:left; }
.pLeft70 { padding-left:70px; }
CODE:
$categories = get_the_category();
foreach($categories as $category) :
	$related_posts = get_posts( 'category='.$category->cat_ID.'&exclude='.$post->ID );
	if($related_posts):
		$args = array (
			'numberposts' => 5,
			'cat' => $category->cat_ID
		);
		$data = get_posts($args);
		
		echo '<aside id="recentEntries"><h3 class="widget-title">最近の投稿</h3>';
		echo "<ul>\n";
		foreach($data as $post) :
			setup_postdata($post);
		?>
		<li class="mBtm10">
			<p class="fLeft">
			<?php /*サムネイル*/
				the_post_thumbnail( array(60,60) ); 
			?>
			</p>
			<p class="pLeft70"><a href="<?php the_permalink(); ?>">
			<?php /*投稿日*/
				the_time('Y年m月d日'); 
			?>
			<br />
			<?php /*タイトル(記事のタイトル:20文字が理想)*/
				the_title(); 
			?>
			</a>

		<?php 
		echo "</li>\n";
		endforeach;
		echo "</ul></aside>\n";
	endif;
endforeach;

wp_reset_postdata(); /*マルチサイトの場合に使用*/


プラグインを使った参考サイト:http://blog.inmycab.com/pc/wordpress/15830/