WordPress中query_posts与WP_Query分页不正确怎么解决
如果你在wordpress中想设置不同分类显示不同数量的文章,并且是用query_posts或者WP_Quer...
最后更新
在functions.php中加入以下代码(可修改样式) 1 2 3 4 5 6 7 8 9 10 11 12…
在functions.php中加入以下代码(可修改样式)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
<?php
function par_pagenavi($range = 9){ //数字9是控制分页数,9是显示十页的意思
global $paged, $wp_query;
$paged = $wp_query->query_vars[‘paged’]; // 当前处于第几页,注意下标从1开始
$max_page = $wp_query->max_num_pages; // 最大页数
if ( !$max_page ) {$max_page = $wp_query->max_num_pages;}
if($max_page > 1){if(!$paged){$paged = 1;}
if($max_page > 1){
echo ‘<div class=”wp-pagenavi”>’; //根据需要可以写样式
echo ‘<span class=”pages”>’; current_paged();
echo ‘</span>’;
//previous_posts_link(‘ Previous ‘);
if($paged != 1){echo ‘<a href=”‘ . get_pagenum_link(1) . ‘” rel=”nofollow”>First Page</a>’;}
if($paged > 1) echo ‘<a href=”‘ . get_pagenum_link($paged–1) . ‘” rel=”nofollow”>Previous</a>’;
if($max_page > $range){
if($paged < $range){
for($i = 1; $i <= ($range + 1); $i++){
if($i==$paged)
echo ‘<span class=”current”>’. $i .‘</span>’;
else
echo ‘<a href=”‘.get_pagenum_link($i) .‘” class=”page larger” rel=”nofollow”>’. $i .‘</a>’;
}
}elseif($paged >= ($max_page – ceil(($range/2)))){
for($i = $max_page – $range; $i <= $max_page; $i++){
if($i==$paged)
echo ‘<span class=”current”>’. $i .‘</span>’;
else
echo ‘<a href=”‘.get_pagenum_link($i) .‘” class=”page larger” rel=”nofollow”>’. $i .‘</a>’;
}
}elseif($paged >= $range && $paged < ($max_page – ceil(($range/2)))){
for($i = ($paged – ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){
if($i==$paged)
echo ‘<span class=”current”>’. $i .‘</span>’;
else
echo ‘<a href=”‘.get_pagenum_link($i) .‘” class=”page larger” rel=”nofollow”>’. $i .‘</a>’;
}
}
}else{
for($i = 1; $i <= $max_page; $i++){
if($i==$paged)
echo ‘<span class=”current”>’. $i .‘</span>’;
else
echo ‘<a href=”‘.get_pagenum_link($i) .‘” class=”page larger” rel=”nofollow”>’. $i .‘</a>’;
}
}
//next_posts_link(‘ Next ‘);
if($paged < $max_page) echo ‘<a href=”‘ . get_pagenum_link($paged+1) . ‘” rel=”nofollow”>Next</a>’;
if($paged != $max_page){echo ‘<a href=”‘ . get_pagenum_link($max_page) . ‘” rel=”nofollow”>Last Page</a>’;}}
echo ‘</div>’;
}
}
function current_paged( $var = ” ) {
global $paged, $wp_query;
$paged = $wp_query->query_vars[‘paged’]; // 当前处于第几页,注意下标从1开始
if( empty( $var ) ) {
global $wp_query;
if( !isset( $wp_query->max_num_pages ) )
return;
$pages = $wp_query->max_num_pages;
}
else {
global $$var;
if( !isset( $$var->max_num_pages ) || !isset( $$var ) )
return;
$pages = absint( $$var->max_num_pages );
}
if( $pages < 1 )
return;
$page = get_query_var( ‘paged’ ) ? get_query_var( ‘paged’ ) : 1;
echo ‘Page Links: ‘ ;
}
function inkthemes_enqueue_scripts() {
if (is_singular() and get_site_option(‘thread_comments’)) {
wp_print_scripts(‘comment-reply’);
}
}
add_action(‘wp_head’, ‘inkthemes_enqueue_scripts’);
?>
|
在需要调取分页的地方加入
1
|
<?php par_pagenavi(9);?>
|
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!