WordpressでTHELOOPを使用して、投稿の代わりにページを読み込む方法はありますか?
子ページのセットをクエリして、その上でLOOP関数呼び出しを使用できるようにしたいthe_permalink()
と思いますthe_title()
。
これを行う方法はありますか?query_posts()
ドキュメントには何も表示されませんでした。
はい、可能です。新しい WP_Query オブジェクトを作成できます。次のようにします。
query_posts(array('showposts' => <number_of_pages_to_show>, 'post_parent' => <ID of the parent page>, 'post_type' => 'page'));
while (have_posts()) { the_post();
/* Do whatever you want to do for every page... */
}
wp_reset_query(); // Restore global post data
追加: query_posts で使用できるパラメーターは他にもたくさんあります。残念ながらすべてではありませんが、一部はhttp://codex.wordpress.org/Template_Tags/query_postsにリストされています。少なくともpost_parent
、より重要なpost_type
ものはそこにリストされていません。./wp-include/query.php
これらについて調べるために、ソースを掘り下げました。
この質問の年齢を考えると、つまずいた人に最新の回答を提供したいと思いました。
query_posts を避けることをお勧めします。私が好む代替案は次のとおりです。
$child_pages = new WP_Query( array(
'post_type' => 'page', // set the post type to page
'posts_per_page' => 10, // number of posts (pages) to show
'post_parent' => <ID of the parent page>, // enter the post ID of the parent page
'no_found_rows' => true, // no pagination necessary so improve efficiency of loop
) );
if ( $child_pages->have_posts() ) : while ( $child_pages->have_posts() ) : $child_pages->the_post();
// Do whatever you want to do for every page. the_title(), the_permalink(), etc...
endwhile; endif;
wp_reset_postdata();
別の方法として、pre_get_posts フィルターを使用することもできますが、これはプライマリ ループを変更する必要がある場合にのみ適用されます。上記の例は、2 次ループとして使用する場合に適しています。