サイトのニュース一覧にWordPress投稿のタイトルやアイキャッチ画像をとりあえず表示させる。
classやスラッグ、ファイル名は仮。
<h2>お知らせ</h2> <dl class="news-list"> <?php $args = array( 'post_type' => 'post', //「投稿」タイプ(カスタム投稿タイプでも◎) 'category_name' => 'news', //カテゴリースラッグ名(特定のカテゴリーのみ表示する場合) 'posts_per_page' => 5 //表示件数(-1で全ての記事を表示) ); $the_query = get_posts( $args ); if ( $the_query ) : foreach ( $the_query as $post ) : setup_postdata( $post ); ?> <div class="image"> <?php if( has_post_thumbnail() ): ?> <!-- WP側のアイキャッチ画像をサムネイルとして表示する --> <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail( $post->ID, 'thumbnail', array( 'class' => 'archive-thumbnail' ) ); ?></a> <?php else: ?> <!-- アイキャッチ画像がないとき代わりに表示する画像 --> <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/thumbnail_default.jpg"></a> <?php endif; ?> </div> <dt> <!-- 日付 --> <?php the_date() ?> </dt> <dd> <p class="news-link"> <!-- タイトル・リンク --> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </p> </dd> <?php endforeach; ?> <?php endif; ?> </dl>