表示中の記事を除いた同カテゴリの新着記事を表示

表示中の記事を除いた同カテゴリーの新着記事を表示
これは通常投稿の場合だけ?かもなので、確認後訂正する。ひとまず覚書きに記載。

<?php $current_id = get_the_ID();
$myposts = get_posts('numberposts=5&category=1&exclude=' . $current_id );
if ($myposts) :
	foreach( $myposts as $post ) :
		setup_postdata( $post ); ?>
		<div class="date"><?php echo date("Y年m月d日", strtotime($post->post_date)); ?></div>
		<h3 class="entry-title"><?php the_title(); ?></h3>
		<div class="entry-content"><?php the_content(); ?></div>
<?php endforeach; endif; ?>

参照ページ
開いている記事を除いて同カテゴリの新着記事を表示

<?php
	$post_id = get_the_ID();
	foreach((get_the_category()) as $cat) {
		$cat_id = $cat->cat_ID ;
		break ;
	}
	query_posts(
		array(
			'cat' => $cat_id,
			'posts_per_page' => 5,
			'post__not_in' => array($post_id)
		)
	);
?>
<?php if (have_posts()) : ?>
<div>
	<h2>関連記事</h2>
	<ul>
<?php while (have_posts()) : the_post(); ?>
		<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
	</ul>
</div>
<?php endif; wp_reset_query(); ?>

参照ページ
関連記事として表示中の記事と同じカテゴリーの一覧を表示

シェアする

フォローする