query_postsを捨てよ、pre_get_postsを使おう
コチラの記事を読んであーそうなんだーとおもっちゃった今日この頃です。
ばりばりquery_posts使っていました。結構このタグ楽なんですよねー、理解しました。
functions.phpに入れてやれば簡単でいいかもですね。
スポンサーリンク
1 2 3 4 5 6 7 8 9 10 11 12 13 | function echo_query_post($query) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_home() ) { $query->set( 'post_type', 'post' ); $query->set( 'posts_per_page', '5' ); $query->set( 'category_name', 'info' ); $query->set( 'orderby', 'date' ); $query->set( 'order', 'DESK' ); } } add_action( 'pre_get_posts', 'echo_query_post' ); |
こんな感じで今は使っています。それで同一ページに2つの投稿を載せたい場合はどうするの?って思ったのですが
やはりここは下記のようにWP_Queryで回してやるしかないようですね。
<?php $q = new WP_Query(array( 'category_name' => 'mayor', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC' )); if ($q->have_posts()) : while($q->have_posts()) : $q->the_post(); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php endwhile; ?> <?php wp_reset_postdata();?> <?php endif; ?> <?php $info = new WP_Query(array( 'category_name' => 'info', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC' )); if ($info->have_posts()) : while($info->have_posts()) : $info->the_post(); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php endwhile; ?> <?php wp_reset_postdata();?> <?php endif; ?> |