将上一个和下一个帖子的特色图片拉入single.php [英] Pull in previous and next post featured images into single.php

查看:80
本文介绍了将上一个和下一个帖子的特色图片拉入single.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的上一篇和下一篇文章设置为无限循环,因此总会有上一篇和下一篇文章. IE.对于最新文章,下一个"帖子将转到最旧的帖子.而且,如果我们正在查看最旧的帖子,则上一个"帖子将转到最新的帖子.

My previous and next posts are set on an infinite loop so there is always a previous and next post. I.E. for the newest article the "next" post would be go to the oldest post. And if we're viewing the oldest post, the "previous" post would go to the newest post.

我需要提取下一篇和上一篇文章的精选图片作为分页导航的背景.

I need to pull in the featured images of the next and previous posts to be the background for the pagination navigation.

当确实有下一篇或上一篇文章时,我会提取图像.但是,当需要无限循环时,即下一个是最旧的帖子,或者上一个是最新的帖子,我的代码就会崩溃.

I've got the images pulling in when there truly is a next or previous post. However, when the infinite loop is necessary, i.e. next being the oldest post, or previous being the newest post, my code breaks down.

我的代码如下.我认为问题在于我实际上并没有在$first_id$last_id中捕获所需的ID.我试过使用get_queried_object_id() $first->post->ID和各种组合来获取由于.previous-title正常工作而被拉入的帖子的ID.

My code is below. I believe the issue is that I'm not actually capturing the ID of what I need in $first_id and $last_id. I've tried using get_queried_object_id() $first->post->ID and various combinations to get the post ID of what is being pulled in since .previous-title is working correctly.

<div class="post_nav">
<?php if( get_adjacent_post(false, '', true) ) : 

    $previous_post = get_previous_post(); ?>

    <div class="previous-img"><?php echo get_the_post_thumbnail( $previous_post->ID );?></div>
    <p class="previous-title"><?php previous_post_link('Previous Story<br> %link','%title');?</p>

<?php else: 
    $first = new WP_Query('posts_per_page=1&order=DESC'); $first->the_post(); 
        $first_id = $first->ID; ?>

        <div class="previous-img"><?php echo get_the_post_thumbnail( $first_id );?></div>
        <?php echo '<p class="previous-title"><a href="' . get_permalink() . '">Previous Story<br>' . the_title() . '</a></p>';

    wp_reset_query(); ?>
<?php endif; ?>


<?php if( get_adjacent_post(false, '', false) ) : 

    $next_post = get_next_post(); ?>

    <div class="next-img"><?php echo get_the_post_thumbnail( $next_post->ID ); ?></div>
    <p class="next-title"><?php next_post_link('Next Story<br> %link','%title');?></p>

<?php else: 
    $last = new WP_Query('posts_per_page=1&order=ASC'); $last->the_post();
        $last_id = $last->ID; ?>

        <div class="next-img"><?php echo get_the_post_thumbnail( $last_id );?></div>
        <?php echo '<p class="next-title"><a href="' . get_permalink() . '">Next Story<br>' . the_title() . '</a></p>';
    wp_reset_query(); ?>
<?php endif; ?>

推荐答案

您是对的,$first$last不是帖子,而是查询.您可以使用get_the_ID()或直接获取ID.

You're right, $first and $last are not posts, but queries. You could either use get_the_ID() or grab the ID directly.

$last_id = get_the_ID();

(由于您已将其设置为活动循环)

( since you've made it the active loop )

$last_id = $last->posts[0]->ID;

这意味着您不需要为$last设置循环,在这种情况下,您还需要通过将ID作为参数的函数来获得永久链接和标题.这是次要的优化,如果您对此感到困惑,请选择第一个选项.

which means you wouldn't need to set up the loop for $last, in which case you'd also need to get the permalink and title via functions taking the ID as a parameter. This is a minor optimization, if you get confused here go with the first option.

此外,else路径中的next-titleprevious-title似乎彼此切换.也许整个代码块都是这样.

Also, your next-title and previous-title in the else paths seem to be switched with each other. Or perhaps the entire code blocks are.

这篇关于将上一个和下一个帖子的特色图片拉入single.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆