【WordPress】特定カテゴリの記事のタイトル、画像、更新日を取得する記述【メモ】

カテゴリ : Wordpress

 

 

5月もドタバタとしているため更新が滞っています。

今回はメモです。

 

 

WordPressを使用してサイト作成等を行う際に、トップページに投稿ページの特定カテゴリの記事を一覧させる記述になります。

記事のタイトル、投稿日、記事本文を取得して表示します。

 

 

また表示させる記事の数や、記事本文の文字数の指定等も行えます。

 

 


<article>
 <h1>特定カテゴリの呼び出し</h1>
 <?php
 $args = array(
 'numberposts' => 3,
 'cat'=> '2'
 );
 $posts = get_posts( $args );
 foreach( $posts as $post ): ?>
 <?php endforeach;
 wp_reset_postdata();?>
 <?php foreach ($posts as $post) : setup_postdata($post); ?>
 <section class="new">
 <?php the_post_thumbnail('rel_tmn', array('alt'=>get_the_title(), 'title'=>get_the_title())); ?>
 <h1><?php the_title(); ?></h1>
 <p><?php echo mb_substr(get_the_excerpt(), 0, 50); ?></p>
 <a href="<?php the_permalink(); ?>" alt="<?php the_title(); ?>">詳細へ</a>
 </section><!--.new1 end-->
 <?php endforeach; ?>
 </article>

 

 

【表示させる投稿の数】

‘numberposts’ => 3,

 

【カテゴリの指定】
‘cat’=> ‘2’

 

 

細かい設定に関しては後日加筆します。。。

 

 

最近の投稿