最近のことで言うとWordPressのバージョンアップデートで、できることがさらに拡充されており、ノーコードで色々できることが増えているのは嬉しいところです。
そんなアップデートでできることが増えている今日この頃ですが、WordPressの関数も使いこなせればWordPress本体の機能でできないこともでき、カスタマイズの自由度も高くなるので、慣れてきたら覚えておくのもいいことです。
この記事では、そんな関数を使って「もっと見るをクリックで表示される記事リストのサンプル」の作り方について解説しています。
トップページやサイドに使えそうなテキストベースの記事一覧を設置したい。
そんな方にオススメの内容です。是非最後までご覧ください。
記事リストのサンプル
早速、記事リストのサンプルです。
-
2026年9月4日
仕事術
社内表彰制度が、その2つの「掛け算」を後押ししていたのかもしれない、という話
-
2026年9月3日
仕事術
セクショナリズムと「功を誇る人」が重なると、組織はゆっくり脆くなるという話
-
仕事術
「間に入って功を誇る人」というタイプについて、今だから分かること
-
2026年9月2日
仕事術
「3ヶ月でWebデザイナー」というコピーに感じる違和感の正体を、自分なりに考えて…
-
2026年9月1日
UIデザイン
「トップへ戻るボタン」問題と、footerに“戻る”を忍ばせるという自分の最近の…
-
仕事術
AI時代のブランド管理は「取り締まる」から「ガードレールを引く」に変わっていく
-
2026年8月31日
JavaScript ネタ帳
IntersectionObserverで、PCのhover×スマホの自動再生に…
-
JavaScript ネタ帳
タブ+Swiperで作る、切り替え式カルーセルUIの実装方法
-
仕事術
案件の進行が長引くのは「作るスピード」のせいじゃない、とAI時代に改めて思う
-
2026年8月30日
仕事術
参議院サイトのリニューアルが話題になったけど、本当の論点は「金額」より「品質」な…
-
仕事術
副業系の「〇ヶ月で月収〇万円」という動画と、少し距離を置いて付き合うようにしてい…
-
仕事術
AIに「作る」を任せられるようになった今、自分が増やしたいのは「考える」時間
-
2026年8月29日
マーケティング活用
フリーランスデザイナーの仕事は「作る」から「伴走する」へ変わっていくかもしれない
-
JavaScript ネタ帳
SwiperのslideTo()とslideChangeで、タブとSwiperを…
-
2026年8月28日
仕事術
AI×デザインに、PhotoshopとIllustratorのような「王道の組み…
-
マーケティング活用
AI時代のデザイナーに必要なのは「作る力」より「判断する力」かもしれない
-
2026年8月27日
UIデザイン
CSSの新機能「クラスプレフィックスセレクタ」で、似た名前のクラスをまとめて指定…
-
2024年4月24日
JavaScript ネタ帳
JavaScriptのgetBoundingClientRect();でカードを…
投稿日付の新しい順に、「投稿日付」「記事カテゴリ」「記事タイトル」が3件表示され、「もっと見る」をクリックすることで、さらに3件づつ表示されます。
JSONは使わずに、ループで記事一覧を出力してjQueryで表示・非表示をいじっているオブジェクトです。
記事リスト実装の方法と手順
それでは、実装の手順と方法についてです。言語は、PHP・jQuery・CSSの3種で、5つの手順です。
これらを順に説明していきます。
記事ページにPHPのコードをそのまま記述をしても、表示されません。
その為、PHPをショートコードで好きな場所で使用できるように準備しておく必要があるので、以下の記事を参考に準備しておきましょう。
これでウィジェットや記事内など、好きな場所にPHPを設置する事ができます。
次に、記事一覧を表示させたい場所。もしくはショートコードで読み込みさせるPHPのファイルに以下のコードを記述します。
<!-- NEWS -->
<section>
<div class="catListBlock">
<div class="catListInner">
<div class="catListInnerBlock">
<ul class="catList">
<?php
global $post;
$lastposts = get_posts( array(
'posts_per_page' => 18,
'post_type' => 'post'
) );
foreach ( $lastposts as $post ) :
setup_postdata( $post );
?>
<li>
<a href="<?php the_permalink(); ?>">
<div class="catTop">
<p class="catDate"><?php the_date(); ?></p>
<p class="catName"><?php $cat = get_the_category(); $cat = $cat[0]; { echo $cat->cat_name; } ?></p>
</div>
<p class="catTitle"><?php
$limit = 40; // 表示させる文字数の上限
if (mb_strlen($post->post_title)>$limit) {
$title= mb_substr($post->post_title,0,$limit) ; echo $title. '…' ;
} else {
echo $post->post_title;
}
?></p>
</a>
</li>
<?php
endforeach;
wp_reset_postdata();
?>
</ul>
<div class="more">
<button>もっと記事を見る</button>
</div>
</div>
</div>
</div>
</section>
<!-- NEWS -->
次に「もっと見る」の挙動を作る為、jQueryのコードを記述します。
以下のコードを<body>〜</body> のクロージングタグ直前に記述します。
var show = 3; //最初に表示する件数
var num = 3; //もっと見るで表示する件数
var contents = '.catList li'; // 対象のlist
$(contents + ':nth-child(n + ' + (show + 1) + ')').addClass('is-hidden');
$('.more').on('click', function () {
$(contents + '.is-hidden').slice(0, num).removeClass('is-hidden');
if ($(contents + '.is-hidden').length == 0) {
$('.more').fadeOut();
}
});
最後に、見た目を整える為にCSSをコピペします。
/* もっと見る */
.catListInnerBlock {
background: #FFF;
border-radius: 3px;
}
ul.catList {
border: none;
list-style: none;
padding: 0;
margin: 0;
}
ul.catList li {
padding: 0;
margin: 0;
}
ul.catList li a {
display: block;
border-bottom: dotted 2px #ddd;
padding: 22px 20px 20px;
text-decoration: none;
}
ul.catList li:last-child a {
border: none;
}
ul.catList li a p {
font-size: 0.95rem;
font-family: Helvetica;
font-weight: 300;
margin-bottom: 0px;
line-height: 1.3;
}
.catTop {
display: flex;
justify-content: flex-start;
flex-wrap: wrap;
align-content: center;
margin-bottom: 5px;
}
p.catDate {
color: #aaa;
}
ul.catList li a p.catTitle {
font-size: 1.05rem;
margin: 0;
font-weight: 400;
line-height: 1.6;
color: #313131;
}
ul.catList li a:hover {
background: #fafafa;
}
ul.catList li a p.catName {
margin-left: 10px;
background: #fafafa;
color: #707070;
font-size: 0.8rem;
padding: 2px 12px;
border-radius: 9999px;
vertical-align: 1px;
display: inline-block;
}
/* もっと見るのボタン */
.catListInnerBlock .more {
text-align: center;
padding-top: 15px;
cursor: pointer;
position: relative;
padding-bottom: 15px;
}
.catListInnerBlock .more:hover {
background: #fafafa;
}
.more button {
position: relative;
border: none;
background: none;
}
.more button:after {
color: #2165c0;
position: relative;
font-family: "Font Awesome 5 Free";
font-weight: 900;
font-size: 1em;
content: "\f055";
padding-left: 10px;
}
/* もっと見るの非表示 */
.catList li.is-hidden {
visibility: hidden;
opacity: 0;
height: 0;
margin: 0 10px;
padding: 0;
}
「もっと見る」のアイコンはFontAwesomeを使っているので、そのまま使う場合はFontAwesome本体も読み込むようにしましょう。
最後に、記事一覧を表示させたい場所に、ショートコードを記述します。
STEP.1 でPHPを読み込ませる設定をして、そこで「textlist.php」というPHPファイルを作った場合は、HTMLに以下のような記述をすればOKです。
[call_php file='textlist']
これで完了です!
コピペ用コード一式
上記の「手順と方法」で紹介した同じコードです。
「詳しい説明等は不要でコピペして使いたい」方は、こちらのコードをそれぞれまるっとコピペすれば完了です。
コードを表示する
<!-- NEWS -->
<section>
<div class="catListBlock">
<div class="catListInner">
<div class="catListInnerBlock">
<ul class="catList">
<?php
global $post;
$lastposts = get_posts( array(
'posts_per_page' => 18,
'post_type' => 'post'
) );
foreach ( $lastposts as $post ) :
setup_postdata( $post );
?>
<li>
<a href="<?php the_permalink(); ?>">
<div class="catTop">
<p class="catDate"><?php the_date(); ?></p>
<p class="catName"><?php $cat = get_the_category(); $cat = $cat[0]; { echo $cat->cat_name; } ?></p>
</div>
<p class="catTitle"><?php
$limit = 40; // 表示させる文字数の上限
if (mb_strlen($post->post_title)>$limit) {
$title= mb_substr($post->post_title,0,$limit) ; echo $title. '…' ;
} else {
echo $post->post_title;
}
?></p>
</a>
</li>
<?php
endforeach;
wp_reset_postdata();
?>
</ul>
<div class="more">
<button>もっと記事を見る</button>
</div>
</div>
</div>
</div>
</section>
<!-- NEWS -->
var show = 3; //最初に表示する件数
var num = 3; //もっと見るで表示する件数
var contents = '.catList li'; // 対象のlist
$(contents + ':nth-child(n + ' + (show + 1) + ')').addClass('is-hidden');
$('.more').on('click', function () {
$(contents + '.is-hidden').slice(0, num).removeClass('is-hidden');
if ($(contents + '.is-hidden').length == 0) {
$('.more').fadeOut();
}
});
/* もっと見る */
.catListInnerBlock {
background: #FFF;
border-radius: 3px;
}
ul.catList {
border: none;
list-style: none;
padding: 0;
margin: 0;
}
ul.catList li {
padding: 0;
margin: 0;
}
ul.catList li a {
display: block;
border-bottom: dotted 2px #ddd;
padding: 22px 20px 20px;
text-decoration: none;
}
ul.catList li:last-child a {
border: none;
}
ul.catList li a p {
font-size: 0.95rem;
font-family: Helvetica;
font-weight: 300;
margin-bottom: 0px;
line-height: 1.3;
}
.catTop {
display: flex;
justify-content: flex-start;
flex-wrap: wrap;
align-content: center;
margin-bottom: 5px;
}
p.catDate {
color: #aaa;
}
ul.catList li a p.catTitle {
font-size: 1.05rem;
margin: 0;
font-weight: 400;
line-height: 1.6;
color: #313131;
}
ul.catList li a:hover {
background: #fafafa;
}
ul.catList li a p.catName {
margin-left: 10px;
background: #fafafa;
color: #707070;
font-size: 0.8rem;
padding: 2px 12px;
border-radius: 9999px;
vertical-align: 1px;
display: inline-block;
}
/* もっと見るのボタン */
.catListInnerBlock .more {
text-align: center;
padding-top: 15px;
cursor: pointer;
position: relative;
padding-bottom: 15px;
}
.catListInnerBlock .more:hover {
background: #fafafa;
}
.more button {
position: relative;
border: none;
background: none;
}
.more button:after {
color: #2165c0;
position: relative;
font-family: "Font Awesome 5 Free";
font-weight: 900;
font-size: 1em;
content: "\f055";
padding-left: 10px;
}
/* もっと見るの非表示 */
.catList li.is-hidden {
visibility: hidden;
opacity: 0;
height: 0;
margin: 0 10px;
padding: 0;
}
ざっくりとしたコードの解説
ざっくりながらコードの解説です。
PHP:記事一覧のループ
記事一覧は、グローバル変数とforeach文を使って出力します。
そこでパラメータを指定して、この記事では以下のようにして「18件」「投稿タイプは通常の投稿」を指定して出力しています。
$lastposts = get_posts( array(
'posts_per_page' => 18,
'post_type' => 'post'
) );
これ以外にも、パラメータを指定することで例えば「並び順を古い順」「ランダムに出力」など色々な条件を設定して出力することができるので、パラメータをいじれるとサイト構築の自由度が上がります。
パラメータについては、関数リファレンス/WP Queryにて詳しい情報が書かれていますが、以下はよく使う内容の抜粋をしているので、気になった方は参考にしてみてください。
jQuery:もっと見る
記事一覧は、PHPで事前に取得した数の記事を展開してjQueryで設定した「表示数」以外の記事に is-hidden のclass名をつけて非表示にしています。
var show = 3; //最初に表示する件数
var num = 3; //もっと見るで表示する件数
「最初に表示する件数」と、「もっと見るで表示する件数」はコメントアウトで記載している通り、冒頭の関数宣言で指定すると、その通りの挙動になります。


