我正在构建一个自定义WordPress主题,我面临以下问题。
如果我创建了一个带有自定义post查询和分页的页面模板,它似乎工作得很好,并按预期呈现分页。但是,如果我将页面作为主题定制的主页,分页似乎不起作用。
如何对此进行排序?
这是我在页面模板中的代码。php
<?php
/* Template Name: Pagebuilder + latest articles + pagination */
get_header();
?>
<?php
if(!empty($post->post_content)) { //show this only when we have content
if (empty($paged) or $paged < 2) { //show this only on the first page
if (have_posts()) { ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="td-container">
<div class="td-container-border">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
<?php }
}
}
?>
<?php
$the_query = new WP_Query( array(
\'posts_per_page\' =>\'2\',
\'paged\' => $paged
));
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li class=smile>
<h3><?php the_title(); ?></h3>
</li>
<?php endwhile; ?>
<?php pagination( $the_query->max_num_pages ); ?>
<?php
get_footer();
这是我的分页函数。
function 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";
}
}