我在Wordpress中有一个自定义的post类型,叫做'cases'。对于单个页面,我创建了一个singlecase.php,它所有的工作都很好,但有一点:
就在While循环之外,我试图有一个"下一个"answers"上一个"链接,就像这样:
<?php next_post_link('%link', 'next item >') ?>
<?php previous_post_link('%link', '< previous item') ?>
但他们陷入了一个循环。一旦它到达某篇文章,Wordpress就会卡住并一遍又一遍地循环相同的两篇文章。
这是完整的(精简的)模板:
... Header ...
<?php while (have_posts()) : the_post(); ?>
<div class="row content">
<div class="span4">
<?php if(count($aImages)) : ?>
<?php foreach($aImages as $aImage) : ?>
....
<?php endforeach; ?>
<?php endif; ?>
</div>
<div class="span7">
<div class="case-title"><?php the_title();?></div>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
... Footer ...
<div class="next-posts pull-right"><?php next_post_link('%link', 'volgend item >') ?></div>
<div class="prev-posts pull-left"><?php previous_post_link('%link', '< vorig item') ?></div>
更新
出于某种原因,这修复了我的问题:
<div class="prev-posts pull-left">
<?php
$prev_post = get_previous_post();
if($prev_post) {
$prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
echo "'t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '" class=" "><strong><<< "'. $prev_title . '"</strong></a>' . "'n";
}
?>
</div>
<div class="next-posts pull-right">
<?
$next_post = get_next_post();
if($next_post) {
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
echo "'t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '" class=" "><strong>"'. $next_title . '" >>></strong></a>' . "'n";
}
?>
</div>
<div class="prev-posts pull-left">
<?php
$prev_post = get_previous_post();
if($prev_post) {
$prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
echo "'t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '" class=" "><strong><<< "'. $prev_title . '"</strong></a>' . "'n";
}
?>
</div>
<div class="next-posts pull-right">
<?php
$next_post = get_next_post();
if($next_post) {
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
echo "'t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '" class=" "><strong>"'. $next_title . '" >>></strong></a>' . "'n";
}
?>
</div>
next_post_link旨在从loop.enter链接描述中调用。由于这是针对单个post的,因此可以将next_post_link和previous_post_liink行安全地移动到循环中。
更新:
不确定你为什么会出现这种行为,但你可以在中添加这样的内容
<?php if ( (get_adjacent_post(false, '', false)) ) { ?>
<div class="next-posts pull-right"><?php next_post_link('%link', 'volgend item >') ?></div>
<?php } elseif ( (get_adjacent_post(false, '', true)) ){ ?>
<div class="prev-posts pull-left"><?php previous_post_link('%link', '< vorig item') ?></div>
<?php } ?>