我的自定义类别页面上的数字分页

时间:2016-02-08 06:17:46

标签: pagination wordpress-theming

我创建了一个带有数字分页的自定义类别页面模板,但问题是我无法查看带有帖子的第3页。

这是我在Function.php中添加的代码



function custom_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 2;
  }
  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }
  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => '/page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('Previous'),
    'next_text'       => __('Next'),
    'type'            => 'plain',
    'add_args'        => False,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class='custom-pagination'>";
      echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
      echo $paginate_links;
    echo "</nav>";
  }

}
&#13;
&#13;
&#13;

我在我的类别中添加的内容-5.php

&#13;
&#13;
<?php

/**

 * Template Name: Custom Page

 * The custom page template file

 */


get_header(); ?>
<div class="container">
<?php 
	$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
	$custom_args = array('post_type' => 'post', 'posts_per_page' => 2, 'paged' => $paged);
	$custom_query = new WP_Query( $custom_args ); ?>
	
		<?php if ( $custom_query->have_posts() ) : ?>
		<!-- the loop -->
		<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
			<article class="loop">
				<h3><?php the_title(); ?></h3>
        		<div class="content">
					<?php the_excerpt(); ?>
				</div>
			</article>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <!-- pagination here -->
    <?php
      if (function_exists(custom_pagination)) {
        custom_pagination($custom_query->max_num_pages,"",$paged);
      }
    ?>
  <?php wp_reset_postdata(); ?>
  <?php else:  ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
  <?php endif; ?>
</div>
<?php get_footer(); ?>
&#13;
&#13;
&#13;

我的CSS

&#13;
&#13;
.custom-pagination span,

.custom-pagination a {

  display: inline-block;

  padding: 2px 10px;

}

.custom-pagination a {

  background-color: #ebebeb;

  color: #ff3c50;

}

.custom-pagination a:hover {

  background-color: #ff3c50;

  color: #fff;

}

.custom-pagination span.page-num {

  margin-right: 10px;

  padding: 0;

}

.custom-pagination span.dots {

  padding: 0;

  color: gainsboro;

}

.custom-pagination span.current {

  background-color: #ff3c50;

  color: #fff;

}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您需要阅读Wordpress分页文档。请阅读以下文档,然后相应地重新创建模板

https://codex.wordpress.org/Pagination https://codex.wordpress.org/Function_Reference/paginate_links

插件:

https://wordpress.org/plugins/wp-paginate/

代码示例;

<强>的functions.php

function pagination($pages = '', $range = 4)
 {  
 $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;
     }
 }   

 if(1 != $pages)
 {
     echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
     if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
     if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";

     for ($i=1; $i <= $pages; $i++)
     {
         if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
         {
             echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
         }
     }

     if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";  
     if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
     echo "</div>\n";
 }
}

风格:

.pagination {
clear:both;
padding:20px 0;
position:relative;
font-size:11px;
line-height:13px;
}

.pagination span, .pagination a {
display:block;
float:left;
margin: 2px 2px 2px 0;
padding:6px 9px 5px 9px;
text-decoration:none;
width:auto;
color:#fff;
background: #555;
}

.pagination a:hover{
color:#fff;
background: #3279BB;
}

.pagination .current{
padding:6px 9px 5px 9px;
background: #3279BB;
color:#fff;
}

现在在模板中使用以下功能,您需要在其中显示分页

<?php if (function_exists("pagination")) {
  pagination($additional_loop->max_num_pages);
} ?>

示例:http://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin

http://www.wpbeginner.com/wp-themes/how-to-add-numeric-pagination-in-your-wordpress-theme/