[2006/09/22] WordPress:Customizable Post Listings

WordPress プラグイン。最新の投稿記事に日付を付けたり、本文テキストの冒頭を抜粋して表示させたり出来ます。

\wp-content\plugins\ 直下にアップロード後、管理画面のプラグインページでActive(有効化)にし、4種類の関数にパラメータと出力フォーマットを組み合わせるだけで色々な記事リストの表現が可能になります。 

4種類の関数(Functions)

function c2c_get_recent_posts ($num_posts = 5, 

  $format = "<li>%post_date%: %post_URL%</li>", 

  $categories = '', $orderby = 'date', 

  $order = 'DESC', 

  $offset = 0, 

  $date_format = 'm/d/Y', 

  $authors = '', 

  $include_passworded_posts = false)

function c2c_get_random_posts($num_posts = 5, 

  $format = "<li>%post_date%: %post_URL%</li>", 

  $categories = '', $order = 'DESC', 

  $offset = 0, 

  $date_format = 'm/d/Y', 

  $authors = '', 

  $include_passworded_posts = false)

function c2c_get_recently_commented ($num_posts = 5, 

  $format = "<li>%comments_URL%<br />%last_comment_date%<br />%comments_fancy%</li>", 

  $categories = '', 

  $order = 'DESC', 

  $offset = 0, 

  $date_format = 'm/d/Y h:i a', 

  $authors = '', 

  $include_passworded_posts = false)

function c2c_get_recently_modified ($num_posts = 5, 

  $format = "<li>%post_URL%<br />Updated: %post_modified%</li>", 

  $categories = '', 

  $order = 'DESC', 

  $offset = 0, 

  $date_format = 'm/d/Y', 

  $authors = '', 

  $include_passworded_posts = false)

パラメータの説明

表示テンプレートに使える percent substitution tags とその説明

コード記述サンプル

最新5件の記事を、日付(西暦.月.日):パーマリンク付き記事タイトル+改行して本文冒頭のn文字(後で説明/備忘録:カスタマイズした点)を降順で表示。(全ての投稿者の記事を表示/パスワード制限のページは表示しない:この2点は記述を省略)

<?php if (function_exists('c2c_get_recent_posts')) { ?>

<ul> <?php c2c_get_recent_posts() ?> </ul>

備忘録:カスタマイズした点

記事本文から冒頭250文字(全角では125文字)までをリストに表示させるには

customizable-post-listings.php 146-151行目参照。下のソースはデフォルト。

//-- Some things you might want to configure -----

$excerpt_words = 6;    // Number of words to use for %post_excerpt_short%

$excerpt_length = 50;     // Number of characters to use for %post_excerpt_short%, only used if $excerpt_words is 0

$idmode = ‘nickname’;    // how to present post author name

$comment_fancy = array(’No comments’, ‘1 Comment’, ‘%comments_count% Comments’);

//-- END configuration section -----

%post_excerpt_short%で文字数を指定できるのは、$excerpt_wordsの値を0にしている時だけ。とのことで、表示させる本文を冒頭から250字までと制限するために、以下の通り変更。

//-- Some things you might want to configure -----

$excerpt_words = 0;    // Number of words to use for %post_excerpt_short%

$excerpt_length = 250;     // Number of characters to use for %post_excerpt_short%, only used if $excerpt_words is 0

$idmode = ‘nickname’;    // how to present post author name

$comment_fancy = array(’No comments’, ‘1 Comment’, ‘%comments_count% Comments’);

//-- END configuration section -----