[WordPress]最近投稿した記事一覧のHtmlリンクを表示する


サイト訪問者に他の記事も読んでもらうため、記事一覧(最新5件)のHtmlリンクをサイドバーに表示できるようにしました。

使用するテンプレートタグ

wp_get_archives

過去記事を取得するにはwp_get_archives()を使用します。get_archives()でもOK(違いは引数の記述方法)。

WPテーマに記述したコード

sidebar.phpに以下のコードを記述。

<div>
<h3>最新の投稿記事</h3>
<ul>
	<? wp_get_archives('type=postbypost&limit=10&format=html'); ?>

</ul>
</div>

実行

上記コードを保存し、ブログを更新します。

実行結果

wp_get_archives.jpg

Html部分のコード

<div>
<h3>最新の投稿記事</h3>
<ul>
	<li><a title="Screengrab" href="https://it-life.kakinota.net/screengrab">Screengrab</a></li>

	<li><a title="Simple Webpage Capture" href="https://it-life.kakinota.net/simple-webpage-capture">Simple Webpage Capture</a></li>
	<li><a title="Internal Server Error 500" href="https://it-life.kakinota.net/internal-server-error-500">Internal Server Error 500</a></li>

	<li><a title="ManicTime 1.1.1を使ってみた" href="https://it-life.kakinota.net/manictime-1-1-1%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%81%bf%e3%81%9f">ManicTime 1.1.1を使ってみた</a></li>
</ul>
</div>

wp_get_archivesの引数

type yearly 年別
monthly 月別(初期値)
weekly 週別
daily 日別
postbypost 記事別
limit 整数 表示したい過去記事数。
format html <li></li>の形式
option <option></option>
link <link>
custom カスタムリスト
before 文字列 formatに「html」または「custom」を指定した場合にリンクタグ(<a>)の直前に追加する文字列。
after 文字列 formatに「html」または「custom」を指定した場合にリンクタグ(<a>)の直後に追加する文字列。
show_post_count 真偽値 過去記事数を表示するか否か。「type」がpostbypost以外の場合に使用できます。