trash-area.com ≫ blog ≫ wordpress ≫ get_posts で「記事」と「ページ」を一度に取得
wordpress の テンプレートタグ「get_posts」を使って
記事とページをまとめて取得して表示する方法です。
テンプレートタグ/get posts – WordPress Codex 日本語版
そもそも「記事とページを一緒に羅列したい!」っていうのが
なんか根本がまちがっているような気がしますけど
こういうのは気にしたら負けですよね。
post_type というパラメータに「any」を指定すると文字通り全てとなります。
とりあえず↑のマニュアルに記載が無かったのでメモです。
以下サンプル。
hoge というカスタムフィールドを持つ「記事」と「ページ」から 5件羅列します。
<ul> <?php $posts = get_posts('numberposts=5&meta_key=hoge&post_type=any'); ?> <?php if($posts): ?> <?php foreach($posts as $post): setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; endif; ?> </ul> |
[…] 投稿日: 2009 年 4 月 22 日 作成者: admin get_posts で「記事」と「ページ」を一度に取得<a href=""> カテゴリー: 未分類 タグ: code […]
[…] get_posts で「記事」と「ページ」を一度に取得 <ul> <?php $posts = get_posts(’numberposts=5&meta_key=hoge&post_type=any’); ?> <?php if($posts): ?> <?php foreach($posts as $post): setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; endif; ?> </ul> […]