WordPressの記述には、記事ループがたくさん出てきます。
よく使う3パターンを備忘録として。
普通に記事ループさせる
ごく普通の記事ループです。
カテゴリー・タグ・日付・アイキャッチ画像・タイトル・抜粋を表示させています。
<ul class="list">
<?php
$args = array(
'posts_per_page' => -1, //表示件数
'category_name' => 'sample' //カテゴリー
);
$posts = get_posts( $args );
foreach ( $posts as $post ):
setup_postdata( $post );
?>
<li>
<a class="post" href="<?php the_permalink(); ?>">
<p class="cate"><?php $category = get_the_category(); $cat_name = $category[0]->cat_name; echo $cat_name; ?></p>
<p class="tag"><?php $tags = get_tags(); if ($tags) { foreach($tags as $tag) { echo $tag->name.''; }} ?></p>
<p class="date"><?php the_time("Y.n.j"); ?></p>
<p class="thumbnail"><?php the_post_thumbnail(); ?></p>
<p class="title"><?php the_title(); ?></p>
<p class="text"><?php echo get_the_excerpt(); ?></p>
</a>
</li>
<?php
endforeach;
wp_reset_postdata();
?>
</ul>
the_date(“Y.n.j”);だと同日更新記事のときに表示されなくなるので、the_time(“Y.n.j”);がおすすめ。
‘posts_per_page’ => -1・・表示させたい個数を記述、「-1」で全件表示
‘category_name’ => ‘sample’・・カテゴリーを絞り込んで表示する場合は記述、空白で全カテゴリー表示
ループする記事がないときにテキストを出す
記事がなかった場合に「記事がありません」と表示したい場合⬇️
<ul class="list">
<?php
$args = array(
'posts_per_page' => 5, //表示件数
'category_name' => 'sample' //カテゴリー
);
$posts = get_posts( $args );
if( !empty( $posts ) ) :
foreach ( $posts as $post ):
setup_postdata( $post );
?>
<li>
<a class="post" href="<?php the_permalink(); ?>">
<p class="cate"><?php $category = get_the_category(); $cat_name = $category[0]->cat_name; echo $cat_name; ?></p>
<p class="tag"><?php $tags = get_tags(); if ($tags) { foreach($tags as $tag) { echo $tag->name.''; }} ?></p>
<p class="date"><?php the_time("Y.n.j"); ?></p>
<p class="thumbnail"><?php the_post_thumbnail(); ?></p>
<p class="title"><?php the_title(); ?></p>
<p class="text"><?php echo get_the_excerpt(); ?></p>
</a>
</li>
<?php
endforeach;
elseif( empty( $myposts )):
?>
<p class="empty">記事がありません</p>
<?php
endif;
wp_reset_postdata();
?>
</ul>
固定ページで記事ループにページネーションをつける
先程の「記事がありません」に加え、固定ページで記事ループさせるときにページネーションをつける場合⬇️
<div class="sample">
<ul class="list">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_Query( array(
'post_status' => 'publish',
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 7, //表示件数
'category_name' => 'sample', //カテゴリー
'orderby' => 'date',
'order' => 'DESC'
) );
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post();
?>
<li>
<a class="post" href="<?php the_permalink(); ?>">
<p class="cate"><?php $category = get_the_category(); $cat_name = $category[0]->cat_name; echo $cat_name; ?></p>
<p class="tag"><?php $tags = get_tags(); if ($tags) { foreach($tags as $tag) { echo $tag->name.''; }} ?></p>
<p class="date"><?php the_date("Y.n.j"); ?></p>
<p class="thumbnail"><?php the_post_thumbnail(); ?></p>
<p class="title"><?php the_title(); ?></p>
<p class="text"><?php echo get_the_excerpt(); ?></p>
</a>
</li>
<?php
endwhile;
else:
echo '<p class="post_empty">記事がありません</p>';
endif;
?>
</ul>
<div class="pagination">
<?php
global $wp_rewrite;
$paginate_base = get_pagenum_link(1);
if(strpos($paginate_base, '?') || !$wp_rewrite->using_permalinks()){
$paginate_format = '';
$paginate_base = add_query_arg('paged','%#%');
}else{
$paginate_format = (substr($paginate_base,-1,1) == '/' ? '' : '/') .
user_trailingslashit('page/%#%/','paged');
$paginate_base .= '%_%';
}
echo paginate_links(array(
'base' => $paginate_base,
'format' => $paginate_format,
'total' => $the_query->max_num_pages,
'mid_size' => 1,
'current' => ($paged ? $paged : 1),
'prev_text' => '< 前へ',
'next_text' => '次へ >',
));
?>
</div>
</div>