自定义帖子类型'single-custom.php'下一个和上一个链接卡在循环中

时间:2014-09-16 15:46:57

标签: php wordpress

我在Wordpress中有一个自定义的帖子类型叫做'个案'。对于单个页面,我创建了一个单case.php,所有这些都是有效的但只有一件事:

在While循环之外,我试图找到一个' Next'和'以前'链接,像这样:

<?php next_post_link('%link', 'next item &gt;') ?>
<?php previous_post_link('%link', '&lt; 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 &gt;') ?></div>
<div class="prev-posts pull-left"><?php previous_post_link('%link', '&lt; 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><<< &quot;'. $prev_title . '&quot;</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>&quot;'. $next_title . '&quot; >>></strong></a>' . "\n";
                    }
    ?>
    </div>

2 个答案:

答案 0 :(得分:7)

<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><<< &quot;'. $prev_title . '&quot;</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>&quot;'. $next_title . '&quot; >>></strong></a>' . "\n";
                }
?>
</div>

答案 1 :(得分:1)

next_post_link意在从循环中调用。enter link description here。由于这是针对单个帖子的,因此您可以安全地将next_post_link和previous_post_link行移动到循环中。

<强>更新

不确定为什么你会得到这种行为,但你可以添加以下内容:

<?php if ( (get_adjacent_post(false, '', false)) ) { ?>
    <div class="next-posts pull-right"><?php next_post_link('%link', 'volgend item &gt;') ?></div>
<?php } elseif ( (get_adjacent_post(false, '', true)) ){ ?>
    <div class="prev-posts pull-left"><?php previous_post_link('%link', '&lt; vorig item') ?></div>
<?php } ?>