2017-06-07 18 views
0

私はカスタムループにページネーションを追加しようとしていますが、どのように行うのかわかりません。 「プレビュー」と「次へ」ボタンを追加すると、同じ10の投稿が常に表示されます。 whileループではなく、foreachループでは解決策がいくつか見つかりました。 これは、ループが(問題get_postsある?)です:カスタムループのページ分割(foreachを使用したループ)

<?php 
    $news = get_posts(array('posts_per_page' => 10)); 
    $news['paged'] = get_query_var('paged') ? get_query_var('paged') : 1; ?> 

     <?php foreach ($news as $article): ?> 
      <div class="col-md-4"> 
       <h3><a href="<?php echo get_permalink($article->ID); ?>"><?php echo $article->post_title ?></a></h3> 
       <hr> 
       <p class="desc"><?php echo $article->post_excerpt ?></p> 

     <a href="<?php echo get_permalink($article->ID); ?>"><?php echo get_the_post_thumbnail($article->ID,'thumbnail'); ?></a> 
       <p class="btn_text"><a href="<?php echo get_permalink($article->ID); ?>"> Ler mais</a></p> 
      </div> 
     <?php endforeach; ?> 


    <?php previous_posts_link('<<'); 
    next_posts_link('>>', $custom_query->max_num_pages); ?> 

答えて

0
<?php 
if (get_query_var('paged')) { 
    $paged = get_query_var('paged'); 
} elseif (get_query_var('page')) { // 'page' is used instead of 'paged' on Static Front Page 
    $paged = get_query_var('page'); 
} else { 
    $paged = 1; 
} 

$custom_query_args = array(
    'post_type' => 'post', 
    'posts_per_page' => get_option('posts_per_page'), 
    'paged' => $paged, 
    'post_status' => 'publish', 
    'ignore_sticky_posts' => true, 
    //'category_name' => 'custom-cat', 
    'order' => 'DESC', // 'ASC' 
    'orderby' => 'date' // modified | title | name | ID | rand 
); 
$custom_query = new WP_Query($custom_query_args); 

if ($custom_query->have_posts()) : 
    while($custom_query->have_posts()) : $custom_query->the_post(); ?> 

     <article <?php post_class(); ?>> 
      <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> 
      <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small> 
      <div><?php the_excerpt(); ?></div> 
     </article> 

    <?php 
    endwhile; 
    ?> 

    <?php if ($custom_query->max_num_pages > 1) : // custom pagination ?> 
     <?php 
     $orig_query = $wp_query; // fix for pagination to work 
     $wp_query = $custom_query; 
     ?> 
     <nav class="prev-next-posts"> 
      <div class="prev-posts-link"> 
       <?php echo get_next_posts_link('Older Entries', $custom_query->max_num_pages); ?> 
      </div> 
      <div class="next-posts-link"> 
       <?php echo get_previous_posts_link('Newer Entries'); ?> 
      </div> 
     </nav> 
     <?php 
     $wp_query = $orig_query; // fix for pagination to work 
     ?> 
    <?php endif; ?> 

<?php 
    wp_reset_postdata(); // reset the query 
else: 
    echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>'; 
endif; 
?>           

出典:http://web-profile.net/wordpress/themes/wordpress-custom-loop/

+0

ありがとうございます!私はまだ問題があります。 「古いエントリ」をクリックすると、私はホームページにリダイレクトされます。 – user3616665

関連する問題