在WordPress的最新版本上,分页仅在首页上有效

时间:2016-01-12 作者:user3463054

“我的分页”在自定义帖子类型的首页上不起作用,当未选择“首页”时,其工作正常。

My code in functions.php

/** pagination **/
function fruit_pagination($pages = \'\', $range = 5)
{  
 $fruit_showitems = ($range * 2)+1;  
 global $paged;
 if(empty($paged)) $paged = 1;
 if($pages == \'\') 
 {
     global $wp_query;
     $pages = $wp_query->max_num_pages;
     if(!$pages)
     {
         $pages = 1;
     }
 } echo \'
 <div class="site-pagination">\';  
 if(1 != $pages)
 {

 echo "
 <ul>
 ";
  if($paged > 2 && $paged > $range+1 && $fruit_showitems < $pages) echo "
 <li class=\'pagination-previous-all\'>
 <a href=\'".get_pagenum_link(1)."\'>
 <span class=\'sprite previous-all-icon\'>First</span></a></li>
  ";
 if($paged > 1 && $fruit_showitems < $pages) echo "
 <li class=\'pagination-previous\'>
  <a href=\'".get_pagenum_link($paged - 1)."\'>
  <span class=\'sprite previous-icon\'>Previous</span></a></li>
   ";
  for ($i=1; $i <= $pages; $i++)
  {
 if (1 != $pages &&( !($i >= $paged+$range+1 || 
 $i <= $paged-$range-1)      || $pages <= $fruit_showitems ))
 {
 echo ($paged == $i)? "
 <li class=\'active\'><a href=\'#\' >".$i."</a></li>
 ":"
 <li><a href=\'".get_pagenum_link($i)."\' class=\'inactive\' >".$i."</a></li>
 ";
  }
 }
 if ($paged < $pages && $fruit_showitems < $pages) echo "
 <li class=\'pagination-next\'>
 <a href=\'".get_pagenum_link($paged + 1)."\'>
  <span class=\'sprite next-icon\'>Next</span></a></li>
   ";  
  if ($paged < $pages-1 &&  $paged+$range-1 < $pages && 
  $fruit_showitems < $pages) echo "
 <li class=\'pagination-next-all\'>
 <a href=\'".get_pagenum_link($pages)."\'>
 <span class=\'sprite next-all-icon\'>Last</span></a></li>
 ";
 echo "
  </ul>
   \\n";
  }
  echo \'</div>
  \';
  }
  /** End pagination **/ 

My code in Custom Page Template and it is selected for Front Page

   global $paged;
    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
                    $rarthemes_html_args = array(
                        \'paged\' => $paged,
                        \'orderby\' => \'post_date\',
                        \'order\' => \'DESC\',
                        \'post_type\' => \'html\',
                        \'post_status\' => \'publish\',
                    );
                    $rarthemes_html = new WP_Query($rarthemes_html_args);
                    while ($rarthemes_html->have_posts())
          { $rarthemes_html->the_post(); the_title(); }

      <div class="col-md-12">
        <?php
                if (function_exists(\'fruit_pagination\')) {

               fruit_pagination($rarthemes_html->max_num_pages, \'2\');
                }
                ?>
      </div>

1 个回复
SO网友:vishal verma

这是forntpage分页的正确工作代码

if(get\\u query\\u var(\'paged\')){

            $paged = get_query_var(\'paged\');

        } elseif ( get_query_var(\'page\') ) {

            $paged = get_query_var(\'page\');

        } else {

            $paged = 1;

        }

        $args = array( 
                  \'post_type\' => \'post\', 
                  \'posts_per_page\' => 8, 
                  \'post_status\'   => \'publish\',
                  \'paged\'          => $paged
         );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();
         the_title(); the_content();
        endwhile;
和分页代码

$大=999999999;//需要不太可能的整数

        echo paginate_links( array(
          \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
          \'format\' => \'?paged=%#%\',
          \'current\' => max( 1, get_query_var(\'page\') ),
          \'total\' => $loop->max_num_pages
        ) );