如何在wordpress中添加自定义分页
How to add Custom pagination in wordpress
我已经在 wordpress 中创建了自定义主题。
我想将自定义分页添加到我的自定义 post 模板,即 INDEX.PHP
你能检查一下分页脚本有什么问题吗?实际上我已经设置了 4 post 限制并且在我的博客中有大约 8 posts.. 当点击 2 分页时,它不会移动到下一页...
<?php get_header(); ?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
// WP_Query arguments
$args = array (
'post_type' => the_post(),
'posts_per_page' => '3',
'paged' => $paged
);
?>
<?php
// The Query
$cquery = new WP_Query( $args );
while ( $cquery->have_posts() ) : $cquery->the_post();
?>
<div class="row">
<div class="img"><a href="<?php the_permalink() ?>" class="imgPos"><?php the_post_thumbnail('full'); ?></a></div>
<div class="text">
<h2><a href="<?php the_permalink() ?>" style="color:#545454;"><?php the_title(); ?></a></h2>
<h3 style="line-height: 1px;"><span class="floatL">By </span> <span class="floatL"> <?php the_author_posts_link(); ?> </span> <span class="floatL"> - </span> <span class="floatL"><?php the_time('F jS, Y'); ?></span><div class="clr"></div></h3>
<p><?php the_content('Read More') ?></p>
</div>
</div>
<?php
$post->ID;
endwhile;
$big = 999999999; // need an unlikely integer
?>
<div class="row">
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( $big, '', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $cquery->max_num_pages
) );
?>
</div>
</div>
<?php get_footer(); ?>
请帮忙:)
谢谢,
哈沙德帕蒂尔
试试这个
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
// WP_Query arguments
$args = array (
'post_type' => 'YOUR_CUSTOM_POST_TYPE',
'posts_per_page' => '3',
'paged' => $paged
);
// The Query
$cquery = new WP_Query( $args );
while ( $cquery->have_posts() ) : $cquery->the_post();
echo $post->ID;
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $cquery->max_num_pages
) );
在 "functions.php" 文件中添加以下代码:
function custom_pagination($pages = '', $range = 2)
{
$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'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</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)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
}
然后在您的 "index.php" 文件中,在 while 循环之后添加 "custom_pagination();"。
我已经在 wordpress 中创建了自定义主题。 我想将自定义分页添加到我的自定义 post 模板,即 INDEX.PHP
你能检查一下分页脚本有什么问题吗?实际上我已经设置了 4 post 限制并且在我的博客中有大约 8 posts.. 当点击 2 分页时,它不会移动到下一页...
<?php get_header(); ?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
// WP_Query arguments
$args = array (
'post_type' => the_post(),
'posts_per_page' => '3',
'paged' => $paged
);
?>
<?php
// The Query
$cquery = new WP_Query( $args );
while ( $cquery->have_posts() ) : $cquery->the_post();
?>
<div class="row">
<div class="img"><a href="<?php the_permalink() ?>" class="imgPos"><?php the_post_thumbnail('full'); ?></a></div>
<div class="text">
<h2><a href="<?php the_permalink() ?>" style="color:#545454;"><?php the_title(); ?></a></h2>
<h3 style="line-height: 1px;"><span class="floatL">By </span> <span class="floatL"> <?php the_author_posts_link(); ?> </span> <span class="floatL"> - </span> <span class="floatL"><?php the_time('F jS, Y'); ?></span><div class="clr"></div></h3>
<p><?php the_content('Read More') ?></p>
</div>
</div>
<?php
$post->ID;
endwhile;
$big = 999999999; // need an unlikely integer
?>
<div class="row">
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( $big, '', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $cquery->max_num_pages
) );
?>
</div>
</div>
<?php get_footer(); ?>
请帮忙:) 谢谢, 哈沙德帕蒂尔
试试这个
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
// WP_Query arguments
$args = array (
'post_type' => 'YOUR_CUSTOM_POST_TYPE',
'posts_per_page' => '3',
'paged' => $paged
);
// The Query
$cquery = new WP_Query( $args );
while ( $cquery->have_posts() ) : $cquery->the_post();
echo $post->ID;
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $cquery->max_num_pages
) );
在 "functions.php" 文件中添加以下代码:
function custom_pagination($pages = '', $range = 2)
{
$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'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</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)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
}
然后在您的 "index.php" 文件中,在 while 循环之后添加 "custom_pagination();"。