特定のカテゴリーの新しいエントリー、新着記事のタイトルをリンク付きでリスト表示する方法。
例:カテゴリーIDが3の場合
<?php $posts = get_posts('category=3&numberposts=0'); global $post; ?> <h2><?php echo get_the_category_by_ID(3) ?></h2> <ul> <?php if($posts): foreach($posts as $post): setup_postdata($post); ?> <li><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endforeach; else : ?> <li>Not Found</li> <?php endif;?> </ul> <?php wp_reset_postdata(); ?>
例:カテゴリースラッグがwordpressの場合
<?php $term_id = get_category_by_slug('wordpress')->term_id; $posts = get_posts('category='.$term_id.'&numberposts=0'); global $post; ?> <h2><?php echo get_the_category_by_ID($term_id) ?></h2> <ul> <?php if($posts): foreach($posts as $post): setup_postdata($post); ?> <li><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endforeach; else : ?> <li>Not Found</li> <?php endif;?> </ul> <?php wp_reset_postdata(); ?>
例:カテゴリー名がワードプレスの場合
<?php $category_id = get_cat_ID( 'ワードプレス' ); $posts = get_posts('category='.$category_id.'&numberposts=0'); global $post; ?> <h2>ワードプレス</h2> <ul> <?php if($posts): foreach($posts as $post): setup_postdata($post); ?> <li><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endforeach; else : ?> <li>Not Found</li> <?php endif;?> </ul> <?php wp_reset_postdata(); ?>