我的主页自定义分页有一个大问题。php页面。它显示了前4篇文章,但当我转到第二页时,我得到一个只有页眉和页脚的页面,但没有新闻内容和侧边栏。
在chrome控制台中,我得到一个错误Failed to load resource: the server responded with a status of 404 (Not Found)
下面是我的代码。我正在从头开始创建一个主题。wordpress似乎会丢失内容。第二页上的php或其他文件。有什么问题吗?
作用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)
{
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link(1)."\'>« First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link($paged - 1)."\'>‹ 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 ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href=\'".get_pagenum_link($pages)."\'>Last »</a>";
echo "</div>\\n";
}
}
首页。php
<div class="main-content">
<div class="container">
<div class="sidebar col-xl-4">
<?php get_template_part( \'templates/sidebar\' ); ?>
</div>
<div class="news-content col-xl-8">
<h1>News</h1>
<?php
$paged = ( get_query_var( \'page\' ) ) ? get_query_var( \'page\' ) : 1;
$args = array(
\'post_type\' => \'news\',
\'posts_per_page\' => 4,
\'paged\' => $paged
);
$custom_query = new WP_Query( $args );
while($custom_query->have_posts()) :
$custom_query->the_post();
?>
<?php if(has_post_thumbnail()) :?>
<div class="news-wrapper col-xl-6">
<div class="news-date">
<?php the_time(\'j.m.Y\'); ?>
</div><!-- .news-date -->
<div class="news-img">
<?php the_post_thumbnail(); ?>
</div><!-- .news-img -->
<a class="news-title" href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a><!-- .news-title -->
<div class="news-text">
<?php echo short_excerpt(25); ?>
</div>
<a href="<?php the_permalink() ?>" class="btn">
read more
</a><!-- .btn -->
</div><!-- .news-wrapper -->
<?php else :?>
<div class="news-wrapper col-xl-6">
<div class="news-date">
<?php the_time(\'j.m.Y\'); ?>
</div><!-- .news-date -->
<a class="news-title" href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a><!-- .news-title -->
<div class="news-text">
<?php echo short_news_content(70); ?>
</div>
<a href="<?php the_permalink() ?>" class="btn">
read more
</a><!-- .btn -->
</div><!-- .news-wrapper -->
<?php
endif;
endwhile;
?>
<?php wp_reset_query(); ?>
<nav class="pagination col-xl-12">
<?php if (function_exists("pagination")) {pagination($custom_query->max_num_pages);} ?>
</nav>
</div><!-- .news-content -->
</div><!-- .container -->
</div><!-- .main-content -->
EDIT
我在挖掘分页出现404错误的原因。我已经删除了我的家。php文件,我只剩下索引了。php witch仅显示页眉和页脚。这正是我在CPTs第二页上看到的。我稍后在else上添加了
no posts
来证实我的怀疑。
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( \'content\', get_post_format() );
endwhile;
else:
echo \'<p>no post</p>\';
endif;
?>
因此,由于某些原因,wordpress无法在索引页上显示我的CPT。你知道怎么解决这个问题吗?
最合适的回答,由SO网友:Dariusz 整理而成
多亏了米洛的评论,我才找到了解决办法。
在我家。php我已删除自定义查询并插入:
<div class="news-content col-12 col-xl-8">
<div class="row">
<h1>News</h1>
</div><!-- .row -->
<div class="row">
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
if(has_post_thumbnail()) :?>
<div class="news-wrapper col-12 col-xl-6">
<div class="news-date">
<?php the_time(\'j.m.Y\'); ?>
</div><!-- .news-date -->
<div class="news-img">
<?php the_post_thumbnail(); ?>
</div><!-- .news-img -->
<a class="news-title" href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a><!-- .news-title -->
<div class="news-text">
<?php echo short_excerpt(25); ?>
</div>
<a href="<?php the_permalink() ?>" class="btn">
czytaj dalej
</a><!-- .btn -->
</div><!-- .news-wrapper -->
<?php else :?>
<div class="news-wrapper col-12 col-xl-6">
<div class="news-date">
<?php the_time(\'j.m.Y\'); ?>
</div><!-- .news-date -->
<a class="news-title" href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a><!-- .news-title -->
<div class="news-text">
<?php echo short_news_content(70); ?>
</div>
<a href="<?php the_permalink() ?>" class="btn">
czytaj dalej
</a><!-- .btn -->
</div><!-- .news-wrapper -->
<?php
endif;
endwhile;
endif;
?>
</div><!-- .row -->
<div class="row">
<?php the_posts_pagination( array( \'mid_size\' => 2 ) ); ?>
</div>
</div><!-- .news-content -->
</div><!-- .container -->
</div><!-- .main-content -->
在我的职能中。我添加的php文件:
add_action( \'pre_get_posts\', \'custom_post_on_homepage\' );
function custom_post_on_homepage( $query ) {
if ( is_home() && $query->is_main_query() )
{
$query->set(\'post_type\', \'news\');
$query->set( \'posts_per_page\', \'4\' );
}
}
希望这对将来的人有所帮助。