query_postsを使わず、pre_get_postsで代用したい。
-
こんばんわ。
wordpress初心者です。query_postsを使いHPを作っていたのですが、ページ送りのプラグインが利用できないので調べてみると、query_postsは使わない方が良い、今はpre_get_postsを使うべきだと書いてありました。
よって、query_postsの部分をpre_get_postsで代用したいのですが、使い方がいまいちわかりません。
このサイトによると、functions.phpを使うとあるのですが、私のサイトの場合どうすればいいのかがわかりません。
現在のindex.phpです。
<?php if (is_front_page()): ?> <?php query_posts( 'category_name=A&posts_per_page=12' ); ?> <div class="title"><a href="http://suliko.moo.jp/?page_id=190">副題</a></div> <?php if (have_posts()) : ?> <?php while (have_posts()): ?> <?php the_post(); ?> <div style="width:240px; height:180;margin:15px; " class="thumb"> <a href="<?php the_permalink(); ?>"> <div class="pic" ><?php the_post_thumbnail('my_thumbnail'); ?></div><h3><?php the_title(); ?></h3></a> </div> <?php endwhile; ?> <?php endif; ?> <?php wp_reset_query(); ?> 同じ物でquery_postsの部分だけが下記のものが下に続く。 <?php query_posts( 'category_name=B&posts_per_page=6' ); ?>
topページにカテゴリーがAのものを12記事、Bのものを6記事のせ、カテゴリーページではAのページではAの記事が18記事、Bも同様。
という作りになっています。(作っているサイトです。
)このサイトを見ながら自分なりにこうかなーと思ってやったのですが(下)、エラーばかりでわかりません。
もしわかる方がいましたら、教えて頂けないでしょうか。よろしくお願いします。
function customize_main_query($query) { if ( is_admin() || ! $query->is_main_query() ) return; $query->is_front_page(){ $query->set( 'category_name=A&posts_per_page=12'); $query->set( 'category_name=B&posts_per_page=6'); } } add_action( 'pre_get_posts', 'customize_main_query' );
4件の返信を表示中 - 1 - 4件目 (全4件中)
4件の返信を表示中 - 1 - 4件目 (全4件中)
- トピック「query_postsを使わず、pre_get_postsで代用したい。」には新たに返信することはできません。