Last active
August 29, 2015 13:56
-
-
Save YoshinoriKobayashi/9135347 to your computer and use it in GitHub Desktop.
WordPress query_posts sub query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// サブクエリ | |
// 検索城乾を指定して、投稿を取得できる。 | |
// *************************************************** | |
//現場で使われているWordPressデザインメソッドより P57 | |
// URLで指定された情報はメインクエリのループで、 | |
// それ以外の情報はサブクエリのループで表示させるというのが基本です。 | |
// 3通りのテンプレートタグ、リセットタグがことなる。 | |
// 作成タグ => リセットタグ | |
// query_posts => wp_reset_query | |
// get_posts => wp_reset_postdata | |
// WP_Query => wp_reset_postdata | |
// 4件を取得 | |
query_posts('posts_per_page=4'); | |
// カテゴリーID3かつ2004年の記事 | |
query_posts('cat=3&year=2004'); | |
// 配列でもOK | |
query_posts(array( | |
'cat' => 3, | |
'year' => 2004 | |
)); | |
// 配列を用いて、投稿と同じタグがつけられている投稿を最大3件取得 | |
$tags = get_the_tags(); | |
$post_tags = array(); | |
if ($tags){ | |
foreach ($post_tags as $post_tag) { | |
$post_tags[] = $post_tag->term_id; | |
} | |
} else { | |
$post_tags[] = 0; | |
} | |
query_posts(array( | |
'posts_per_page' => 3, | |
'tag__in' => $post_tags | |
)); | |
//*********************************************** | |
// WordPressの教科書2 | |
// P99 | |
// 投稿タイプが「投稿」のすべての記事を取得 | |
// paged ページ送りのページ数を指定します。無指定の場合は先頭のページ | |
query_posts(array('post_type' => 'post','paged' => get_query_var('paged'))); | |
if (have_posts()) : | |
while (have_posts()): | |
the_post(); | |
get_template_part('content-archive'); | |
endwhile; | |
endnif; | |
if (class_exists('WP_SiteManager_page_navi')): | |
WP_SiteManager_page_navi::page_navi(); | |
endif; | |
// query_postsでの条件をクリア | |
wp_reset_query(); | |
// P104 | |
// 最近の投稿一覧を表示 | |
query_posts(array('post_type' => 'post')); | |
while (have_posts()) : | |
the_post(); | |
get_template_part('content-top-info'); | |
endwhile; | |
wp_reset_query(); | |
// P123 | |
// orderby 並び替えの基準を指定しています。 | |
// menu_orderを指定して、ページ属性の順序に基づくように指定 | |
query_posts(array('post_type' => 'branch','orderby' => 'menu_order','order' => 'ASC','posts_per_page' => 0)); | |
// P150 | |
// 投稿タイプのtourの一覧取得に変更 | |
query_posts(array('post_type' => 'tour','paged' => get_query_var('paged'))); | |
if (have_posts()): | |
while (have_posts()): | |
the_post(); | |
get_template_part('content-archive-tour'); | |
endwhile; | |
endif; | |
if (class_exists('WP_SiteManager_page_Navi')): | |
WP_SiteManager_page_navi::page_navi(); | |
endif; | |
wp_reset_query(); | |
// P164 | |
// 投稿タイプのtourの一覧6件の取得に変更 | |
query_posts(array('posts_type' => 'tour','posts_per_page' => 6)); | |
while (have_posts()): | |
the_post; | |
get_template_part('content-to-tour'); | |
endwhile; | |
wp_reset_query(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment