我在 Wordpress 中有一个名为“案例”的自定义帖子类型。对于单个页面,我创建了一个 single-case.php,它可以正常工作,但有一点:
在 While 循环之外,我正在尝试使用“下一个”和“上一个”链接,如下所示:
<?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>
关于php - 自定义帖子类型 'single-custom.php' 下一个和上一个链接卡在循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25873179/