2016-05-14 15 views
0

私はワードプレスのテーマにいくつかのコードを追加して、ページの下部にページ番号を表示しようとしています。私はソースコードを探していたときwpでページネーションを実装しても動作しません

<main id="main"> 

      <?php 
      // the query 
      $args = array('posts_per_page' => 2); 
      $the_query = new WP_Query($args); 

      ?> 

      <?php if ($the_query->have_posts()) { ?> 

       <!-- loop --> 

       <?php while ($the_query->have_posts()) { 

          $the_query->the_post(); ?> 
     <article id="post"> 

        <div id="thumbnail"> 

         <?php 
         if (has_post_thumbnail()) { 
          the_post_thumbnail(); } ?> 

       </div> 

       <h2><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h2> 

       <div class="entry"> 

        <?php the_excerpt(); ?> 

       </div> 


     </article> 


      <?php } } else { ?> 
      <p><?php _e('Die Posts entsprechen nicht den Kriterien.'); ?></p> 
      <?php } ?> 

      <!-- pagination --> 
       <?php 
    if($the_query->max_num_pages>1){?> 
     <p class="paged"> 
     <?php 
      if ($paged > 1) { ?> 
      <a href="<?php echo '?paged=' . ($paged -1); //prev link ?>"><</a> 
          <?php } 
     for($i=1;$i<=$the_query->max_num_pages;$i++){?> 
      <a href="<?php echo '?paged=' . $i; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a> 
      <?php 
     } 
     if($paged < $the_query->max_num_pages){?> 
      <a href="<?php echo '?paged=' . ($paged + 1); //next link ?>">></a> 
     <?php } ?> 
     </p> 
    <?php } ?> 
<!-- end pagination -->  


      <!-- end of the loop --> 


      <?php wp_reset_postdata(); ?> 


    </main> 

私は改ページを見つけることができません: は、ここにページネーションと私のループです。私は間違って何をしていますか?答えを見つけることができません、私のためのコードはありません。誰かが私を助けることができればいいだろう。 :)

答えて

0

だけで、これらの機能を使用し、自動的に次と前のリンクを表示するには:

next_posts_link(); 
previous_posts_link(); 

https://codex.wordpress.org/Paginationはこれを助けることができます。カスタムでは、それはそれはのように見えると思いますので、$、無制限のページのゼロとして変数をMAX_PAGES個に渡すことができますループ:

next_posts_link("Older Posts", 0); 
1

使用し、デフォルトのWordPressのページネーション、ここでは一例です:

<?php 
// set the "paged" parameter 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$args = array(
    'posts_per_page' => 5, 
    'paged' => $paged, 
); 
$the_query = new WP_Query($args); 
if ($the_query->have_posts()) : 
// the loop 
    while ($the_query->have_posts()) : $the_query->the_post(); 
     the_title(); 
    endwhile; 

// Pagination 
echo get_next_posts_link('Older', $the_query->max_num_pages); 
echo get_previous_posts_link('Newer'); 

// clean up after our query 
wp_reset_postdata(); 

else: 
_e('Sorry, no posts matched your criteria.'); 
endif; 
0

場合ページリンクを含めるには、次の/前のリンクではなく< 1 2 3>の形式でページリンクを含めるには、WordPress関数paginate_linksを使用します。

だからあなたの例のコードは次のようになります。

<?php 
//query 
$paged = (isset($_REQUEST['paged']) && $_REQUEST['paged'] > 0 ? $_REQUEST['paged'] : max(1, get_query_var('paged'))); 
$args = array(
    'posts_per_page' => 2, 
    'paged' => $paged, 
); 
$the_query = new WP_Query($args); 

//loop 
if ($the_query->have_posts()) { 
    while ($the_query->have_posts()) { 
     $the_query->the_post(); 

     //display your post here 
    } 

    //pagination 
    if($the_query->max_num_pages > 1){ ?> 
     <div class="paged"> 
      <?php 
      $big = 999999999; // need an unlikely integer 

      echo paginate_links(array(
       'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), 
       'format' => '?paged=%#%', 
       'current' => $paged, 
       'total' => $the_query->max_num_pages, 
       'prev_text' => __('« Previous'), 
       'next_text' => __('Next »'), 
      )); 
      ?> 
     </div> 
    <?php } 

} else { 
    echo '<p>'._e('Die Posts entsprechen nicht den Kriterien.').'</p>'; 
} 
wp_reset_postdata(); 
?> 
関連する問題