web-dev-qa-db-ja.com

Get_postsを使ったWordpressのページ付け

恐ろしく書かれたWPテーマ(テーブルに書かれたカスタムテーマ、そして悪いコード)を調整する必要があります。

テーマにはいくつかのカスタムテンプレートがありますが、ページネーションは使用されず、get_postsはquery_postsの代わりに使用されました -

    <?php query_posts('showposts=1'); ?>
    <?php $posts = get_posts('numberposts=10&offset=0&category_name=albertsons, carrs, dominicks, genuardis, heb, kroger, pavillions, publix, randalls,safeway,shop-rite,tom-thumb,vons,whole-foods'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count2 = 0; if ($count2 == "10") { break; } else { ?>

...

    <?php $count2++; } ?>
    <?php endforeach; ?>

私は'paged' => get_query_var('page')を追加できるように、ページ付けをget_postsで動作させるか、query_postsのみを使用するように関数を書き換える必要があります。

私がquery_postsだけを使うように書き直そうとすると、いまいましいこと全部が壊れます。

更新:

<?php 
global $wp_query;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;


query_posts(array('posts_per_page' => '3','paged'=>$paged,'category_name'=>'albertsons, carrs, dominicks, genuardis, heb, kroger, pavillions, publix, randalls,safeway,shop-rite,tom-thumb,vons,whole-foods')); ?>
                    ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

これはループを示していますが、ページ付けは機能していません。 「古い投稿」をクリックすると、URLがpage-2に変わりますが、内容はまったく同じです。

1
Jason

これはあらゆる種類の間違いです。最初はstart_wp();を使わないでください私はそれが4年前に減価償却されたと思います。第二にあなたのループはめちゃくちゃです、query_postsはメインループを変更するためのもので、get_postsとは別名です。

ですので通常はget_postsまたはWP Queryを使って書いてください。

$args = array( 'numberposts' => 1, 
               'offset'=> 0, 
               'category_name' => 'carrs, dominicks, genuardis, etc', 
                );


$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) : $the_query->the_post();

$count_posts = $the_query->current_post + 1; // use this to count your posts

//your loop stuff
endwhile;

どのようにページ付けをしたいのかよくわからない場合は、次の/前のリンクに本当の番号付きページ付けに<?php previous_post(); ?> <?php next_post(); ?>を使用したい場合は、WP-PageNaviやWP-Paginateのようなプラグインをお勧めします.

http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Next_and_Previous_Links

1
Wyck