【WordPress】記事をページにループ表示させる方法

WordPress

■管理画面「投稿」→「カテゴリー」より、新規カテゴリを追加

■カテゴリー名:カテゴリ名を記載
■スラッグ名:URLに適した形式の名前(英数字ハイフンのみ)

■ページ内に下記ソースを追加(一例)

<?php
$newslist = get_posts( array(
'category_name' => 'news', //特定のカテゴリースラッグを指定
'posts_per_page' => 15 //取得記事件数
));
foreach( $newslist as $post ):
setup_postdata( $post );
?>

<ul>
<!-- ▼各記事日付/タイトル▼ -->
<li><?php the_time('Y/n/j'); ?> : </li><!-- 投稿日 -->
<li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li><!-- 記事リンクURLと記事タイトル -->
<!-- ▲各記事日付/タイトル▲ -->
</ul>

<?php
endforeach;
wp_reset_postdata();
?>

記事情報を呼び出すソース一覧

// 記事投稿日
<?php the_time('Y/n/j'); ?>

// 記事タイトル
<?php the_title();?>

// サムネイル(アイキャッチ)画像
<?php the_post_thumbnail('画像サイズのID'); ?>

// 各記事リンクURL
<a href="<?php the_permalink(); ?>">●●●</a>

// 記事本文抜粋
<?php the_excerpt();?>

記事本文の抜粋及び文字数指定

■文字数制限は「function.php」に下記を記載

function new_excerpt_mblength($length) {
return 110;
}
add_filter('excerpt_mblength', 'new_excerpt_mblength');

■抜粋したテキストの最後に「続きを読む」は「function.php」に下記を記載

function new_excerpt_more($more) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');

Copyright (C) 2017 N-PLUS All right reserved.