前回の授業では特定の記事を表示させる為に以下のように「query_posts」を使いましたが、現在「query_posts」は非推奨なので、代わりに「get_posts」を使った形に変更しましょう。
「query_posts」の場合
<dl> <?php query_posts('cat=2&showposts=4'); if ( have_posts() ) : while ( have_posts() ) : the_post(); $cf = get_post_meta($post->ID); $recent_class = 'popular_post_box recent-'.$i; ?> <a href="<?php the_permalink(); ?>"> <dt><?php the_time('Y'); ?>.<?php the_time('m'); ?>.<?php the_time('d'); ?> <span class="blog-cate"> <?php $cat = get_the_category(); $cat = $cat[0]; { echo $cat->cat_name; } ?> </span> </dt> <dd class="blog-txt"><?php the_title(); ?></dd> </a> <?php $i++; endwhile; endif; ?> </dl>
「get_posts」の場合
ニュースカテゴリー記事を出力
<dl> <?php $arg = array( 'posts_per_page' => 4, // 表示する件数 'orderby' => 'date', // 日付でソート 'order' => 'DESC', // DESCで最新から表示、ASCで最古から表示 'category_name' => 'news' // 表示したいカテゴリーのスラッグを指定 ); $posts = get_posts( $arg ); if( $posts ): ?> <?php foreach ( $posts as $post ) : setup_postdata( $post ); ?> <a href="<?php the_permalink(); ?>"> <dt><?php the_time( 'Y.m.d' ); ?></dt> <dd><?php the_title(); ?></dd> </a> <?php endforeach; ?> <?php endif; wp_reset_postdata(); ?> </dl>
コメント