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

标签 php wordpress

我在 Wordpress 中有一个名为“案例”的自定义帖子类型。对于单个页面,我创建了一个 single-case.php,它可以正常工作,但有一点:

在 While 循环之外,我正在尝试使用“下一个”和“上一个”链接,如下所示:

<?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>

最佳答案

<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>

关于php - 自定义帖子类型 'single-custom.php' 下一个和上一个链接卡在循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25873179/

相关文章:

php - 带有关键字 Interface , extends , implements 的面向对象的 PHP

wordpress - WooCommerce 破坏了 Wordpress 管理员

php - WordPress - 检查用户是否登录

wordpress - woocommerce_coupon_get_discount_amount 的折扣金额不正确

php - 对在数据库中存储数据的表单字段添加最大字符数限制是个好主意吗?

javascript - php 变量正在更新,但 javascript 函数始终显示第一个值

php - 如何让视频播放速度更快?

php - 是否有一个 php 框架可以更轻松地使用 jquery 和 ajax?

php - 无法将文本与容器中的图像对齐

forms - Mailpoet 自定义表单和自定义字段