2016-02-08 59 views
0

我已經創建了一個帶有數字分頁的自定義分類頁面模板,但問題是我無法查看帶有文章的第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>"; 
 
    } 
 

 
}

我在品類5.php添加什麼

<?php 
 

 
/** 
 

 
* Template Name: Custom Page 
 

 
* The custom page template file 
 

 
*/ 
 

 

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

我的CSS

.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; 
 

 
}

回答

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/