get_posts関数の使い方とCSS Template Listsのページでやってることを紹介しますん。
CSS Template Listsではカテゴリーに記事がpublicで投稿されたら、記事のサムネイルになる画像にリンク貼って一覧表示しています。
記事のサムネイルについては設定や表示をするプラグインもありますが、プラグイン使わなくてもやれる方法があるよ、ってことで。
あるカテゴリーの記事をリストアップしたいという場合、この記事のサンプルのように
普通はquery_posts()とか使うと思いますが
1 2 3 4 5 6 7 | <?php query_posts( 'cat=5' ); if (have_posts()) :?> <?php while (have_posts()) : the_post(); ?> <div class = "post" > <?php the_content(__( 'Read More »' )); ?> </div> <?php endwhile ;?> <?php wp_reset_query(); endif ;?> |
実際使ってみると出来ないことの方が多い。ので、
以前Post, Page, and Attachment Functionsでちらっと書いた
queryよりもスマートに取得出来る、get_posts()を使用する。
※カスタマイズ初心者には難しいかもしれない
get_posts()を使うとデータがオブジェクト形式で返ってきます。
適当にページテンプレートを作って以下のソースで試してみてください。
1 2 3 4 5 6 7 | <?php $myposts = get_posts(); print "<pre>" ; print_r( $myposts ); print "</pre>" ; ?> |
上記ソースはループの中と外、どちらでも使えます。
帰ってくるオブジェクトはこんな感じ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | Array ( [0] => stdClass Object ( [ID] => 1 [post_author] => 1 [post_date] => 2008-06-13 12:04:15 [post_date_gmt] => 2008-06-13 03:04:15 [post_content] => WordPress へようこそ。これは最初の投稿です。編集もしくは削除してブログを始めてください ! [post_title] => Hello world! [post_category] => 0 [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => hello-world [to_ping] => [pinged] => [post_modified] => 2008-06-13 12:04:15 [post_modified_gmt] => 2008-06-13 03:04:15 [post_content_filtered] => [post_parent] => 0 [guid] => http: //localhost/wordpress/?p=1 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 1 ) ) |
配列の中に記事の情報がオブジェクト形式で並んでる状態なので、
各記事ごとに処理をする場合は次のように繰り返し文を使用する。
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $myposts = get_posts(); print "<ul>" ; foreach ( $myposts as $post ){ print "<li>" ; print $post ->post_title; print "</li>" ; } print "</ul>" ; ?> |
要素の値を取り出す場合は$post->post_title;のような書き方をします。
配列だと$post[‘post_title’]で、オブジェクトだと$post->post_title;。
get_posts()のパラメータは公式の解説にもありますが、次の通り。
- $numberposts
- (integer)(オプション) 返す記事の数。デフォルト: 5
- $offset
- (integer)(オプション) 最新の投稿からのオフセット。デフォルト:0
- $category
- (integer) (オプション) 指定したカテゴリIDの投稿のみ表示する。
(複数同時設定は出来ない)デフォルト:なし - $orderby
-
- 'post_title' – 記事のタイトルのアルファベット順
- 'post_date' - 投稿日順
- 'post_modified' – 更新日順
- 'ID' – 記事ID順.
- 'post_author' – 記事作成者のID順
- 'post_name' – 記事スラッグのアルファベット順
補足: $orderbyの値はwp_posts tableのいくつかのフィールド名が使えます。デフォルト: post_title
- $order
- (string)(オプション)$orderbyでの並べ方。
- 'ASC' – 昇順 (低→高)
- 'DESC' – 降順 (高→低)
デフォルト: ASC
- $include
- (string) (オプション) 出力したい記事をIDで指定する。複数指定する場合はカンマか半角スペースで区切る。
以下の値は6つの記事を出力します。- ‘45,63, 78 94 ,128 , 140’
補足:このパラメータを使用すると、numberposts, offset, category, exclude, meta_key, meta_value, post_parentを上書きします。
デフォルト:なし - $exclude
- (string) (オプション) 除外する記事をIDで指定する。$includeと同じ。
デフォルト:なし - $meta_key and $meta_value
- (string) (オプション)
指定したカスタムフィールドのキーまたは値がある投稿だけ表示する。
両方のパラメータを定義する必要がある。 - $post_type
- (string) (オプション) 記事のタイプ。
- post – デフォルト
- page
- attachment
- (blank) – 全ての記事タイプ
- $post_status
- (string) (オプション) 記事のステータス。
- publish - デフォルト(公開)
- private(非公開)
- draft(下書き)
- future(予約)
- (blank) – 全てのタイプ
- $post_parent
- (integer) (オプション) 指定したIDの子記事だけ表示する
(string) (オプション) 並べ替えの指定。
以下にサンプルをいくつか紹介します。
ランダムで記事を表示
1 2 3 4 5 6 7 8 9 | <h2>A random selection of my writing</h2> <ul> <?php $rand_posts = get_posts( 'numberposts=5&orderby=RAND()' ); foreach ( $rand_posts as $post ) : ?> <li><a href= "<?php the_permalink(); ?>" ><?php the_title(); ?></a></li> <?php endforeach ; ?> </ul> |
※ループ外に置く必要があります
全てのメディアファイルを表示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php $args = array ( 'post_type' => 'attachment' , 'numberposts' => null, 'post_status' => null, 'post_parent' => null, // any parent ); $attachments = get_posts( $args ); if ( $attachments ) { foreach ( $attachments as $post ) { setup_postdata( $post ); the_attachment_link( $post ->ID, false); } } ?> |
※アップロードされたメディアファイルを一覧表示する。ループの中と外どちらでもOK
このソースは、画像以外のファイルがアップロードされているとそれも表示してしまいます。
記事サムネイル表示について
CSS Template Listsのページで表示してる画像は、各記事に画像ファイル名をカスタムフィールドに登録しています。
ex:key=thumb、値=sample.gif
記事用の画像を任意のフォルダにFTPとかでアップしといて、get_postsを使った関数でリストを作成して出力すれば一覧表示が出来ます。
ただ、get_postsのパラメータでカテゴリーを指定する場合、複数同時に指定することは出来ません。
いくつか方法はありますが、次のサンプルソースは関数へ渡す引数をカテゴリIDにしています。
カスタムフィールドの値はget_post_meta()で取得します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function get_postthumb_lists( $cat ){ $myposts = get_posts( 'numberposts=null&category=' . $cat ); if (! $myposts ) { print "<p class=\"nodata\">No Data</p>" ; } foreach ( $myposts as $post ){ $title = $post ->post_title; $excerpt = $post ->post_excerpt; $thumb = get_post_meta( $post ->ID, 'thumb' , true); $page = $post ->guid; print "<p><a href=\"" . $page . "\" title=\"View Post\">" . $title . "</a><br />" ; print "<a href=\"" . $page . "\" title=\"" . $excerpt . "\"><img src=\"http://sample.wp.jp/images/" . $thumb . "\" width=\"120\" height=\"80\" alt=\"" . $title . "\"></a></p>\n" ; } } |
上記ソースをfunction.phpかテンプレートに入れて、
1 | <?php get_postthumb_lists(10); ?> |
と書くと、書いた場所に指定したカテゴリの記事一覧が出ます。
画像サイズがバラバラなら、getimagesize()を使用するといいかも。
category.phpとかindex.phpでサムネイルを表示したい場合は、ループ内でget_post_meta()を使用すればおk。
この場合の$postはグローバル変数。
1 2 | $thumb = get_post_meta( $post ->ID, 'thumb' , true); print "<a href=\"" .the_permalink(). "\"><img src=\"http://sample.wp.jp/images/" . $thumb . "\" width=\"120\" height=\"80\" alt=\"" . $title . "\"></a></p>\n" ; |
post_categoryフィールドについて
※2009-06-30追記
[post_category]の値をget_cat_name()とかの引数にすればカテゴリーの値が得られるっぽくみえますが、
このフィールドはver2.8で廃止され、2.7以前のバージョンでは非推奨扱いになっているため全ての記事で値が0になっています。
カテゴリーの情報を得たい場合はwp_get_post_categories()と記事IDを使用します。
「[WP]get_posts()関数について」への1件のフィードバック