【发布时间】:2016-08-10 06:55:46
【问题描述】:
我尝试使用 WP_Query 向我的自定义页面添加一个简单的分页。但是,当我点击下一页链接后,它总是会转到 index.php(因为我的主题中仍然没有 404.php)
这是包含分页的自定义页面的代码
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=2&cat=2'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="col-md-3 col-sm-4 article-list" data-sr='enter bottom, move 50px, over 0.5s'>
<a href="<?php the_permalink(); ?>"><p class="article-link-img"><?php the_post_thumbnail('small-thumbnail'); ?></p>
<p class="article-link-title"><?php the_title(); ?></p></a>
</div>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
这是我 index.php 中的代码
<?php get_header(); ?>
<div class="container index-section">
<section>
<div class="row">
<div class="col-md-12">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php endwhile; else: ?>
<h1 class="index-error-message"><?php _e('Sorry, this post does not exist.'); ?></h1>
<h2 class="single-article-popular-title">Popular Articles</h2>
<?php
$topBlogs = new WP_Query('orderby=rand&cat=3&posts_per_page=4');
if ($topBlogs -> have_posts() ) :
while ($topBlogs -> have_posts() ) : $topBlogs -> the_post(); ?>
<div class="col-md-3 col-sm-3 col-xs-6 single-article-popular-list">
<a href="<?php the_permalink(); ?>"><p><?php the_post_thumbnail('small-thumbnail'); ?></p>
<p class="single-article-popular-link-title"><?php the_title(); ?></p></a>
</div>
<?php endwhile; else: ?>
<h3>No content found</h3>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
</section>
</div>
<?php get_footer(); ?>
我试图找出问题所在,但仍然没有解决方案。我也试过用WP_PageNAVi插件,结果还是一样。
这是我的自定义页面的完整代码
<?php get_header(); ?>
<div class="article-featured">
<div class="container">
<section>
<div class="row">
<?php
$featuredBlogs = new WP_Query('cat=4&posts_per_page=1');
if ($featuredBlogs -> have_posts() ) : while ($featuredBlogs -> have_posts() ) : $featuredBlogs -> the_post(); ?>
<div class="col-md-12 article-featured-descr">
<a href="<?php the_permalink(); ?>"><p class="article-featured-descr-text" data-sr='enter bottom, move 50px, over 0.5s'>Featured article</p>
<h1 data-sr='enter bottom, move 50px, wait 0.3s, over 0.5s'><?php the_title(); ?></h1>
<img src="/ecteciant/wp-content/themes/ecteciant/files/images/article/no-article.png" alt="no article" data-sr='enter bottom, move 50px, wait 0.6s, over 0.5s'></a>
</div>
<?php endwhile; else: ?>
<div class="col-md-12 article-featured-descr">
<p class="article-featured-descr-text" data-sr='enter bottom, move 50px, over 0.5s'>No featured article found</p>
<img src="/ecteciant/wp-content/themes/ecteciant/files/images/article/no-article.png" alt="no article" data-sr='enter bottom, move 50px, wait 0.3s, over 0.5s'>
</div>
<?php endif; ?>
</div>
</section>
</div>
</div>
<div class="container">
<section>
<div class="row">
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=2&cat=2'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="col-md-3 col-sm-4 article-list" data-sr='enter bottom, move 50px, over 0.5s'>
<a href="<?php the_permalink(); ?>"><p class="article-link-img"><?php the_post_thumbnail('small-thumbnail'); ?></p>
<p class="article-link-title"><?php the_title(); ?></p></a>
</div>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
<div class="col-md-12 article-not-found">
<img src="/ecteciant/wp-content/themes/ecteciant/files/images/article/submit-article.png" alt="submit article" data-sr='enter bottom, move 50px, over 0.5s'>
<p data-sr='enter bottom, move 50px, wait 0.3s, over 0.5s'>What do you think about <b>"A"</b>? How about <b>"B"</b>?<br/>Send your thoughts to <a href="mailto:article@ecteciant.com?Subject=New%20Article%20Inquiry">article@ecteciant.com</a>, we accept from <b>A to Z.</b> Our team will review your submission.<br/>Stand a chance to be <b>FEATURED</b> in our page.</p>
</div>
</div>
<!--<iframe src="https://www.facebook.com/plugins/share_button.php?href=https%3A%2F%2Fbudilokawijaya.com&layout=button_count&mobile_iframe=true&width=86&height=20&appId" width="86" height="20" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>
<a href="https://twitter.com/intent/tweet?button_hashtag=ecteciantID" class="twitter-hashtag-button" data-related="budi_czras,ecteciantID" data-url="http://ecteciant.com">Tweet #ecteciantID</a>-->
</section>
</div>
<?php get_footer(); ?>
【问题讨论】:
标签: php wordpress pagination