UnderScotres.me在post_nav函数中检索下一个/上一个帖子缩略图

时间:2014-02-12 作者:mtwallet

我使用下划线。我来做一个项目。我想将特色图片(缩略图)添加到上一篇/下一篇文章的链接中。他们有一个我正在编辑的函数叫做THEMENAME_post_nav(). 我正在努力调整此函数,使其包含缩略图。有人能帮忙吗?代码如下:

function THEMENAME_post_nav() {
// Don\'t print empty markup if there\'s nowhere to navigate.
$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, \'\', true );
$next     = get_adjacent_post( false, \'\', false );

// This is how I\'m trying to include the thumbnails
$prevthumbnail = get_the_post_thumbnail($previous->ID);
$nextthumbnail = get_the_post_thumbnail($next->ID);

if ( ! $next && ! $previous ) {
    return;
}
?>
<nav class="navigation post-navigation" role="navigation">
    <h1 class="screen-reader-text"><?php _e( \'Post navigation\', \'THEMENAME\' ); ?></h1>
    <div class="nav-links">
        <?php

            previous_post_link( \'<div class="nav-previous">%link</div>$prevthumbnail\', _x( \'<span class="meta-nav">&larr;</span> %title\', \'Previous post link\', \'THEMENAME\' ) );
            next_post_link( \'<div class="nav-next">%link</div>$nextthumbnail\', _x( \'%title <span class="meta-nav">&rarr;</span>\', \'Next post link\', \'THEMENAME\' ) );
        ?>
    </div><!-- .nav-links -->
</nav><!-- .navigation -->
<?php
} 

2 个回复
最合适的回答,由SO网友:mtwallet 整理而成

我通过使用get_the_post_thumbnail() 而不是变量$prevthumbnail$nextthumbnail.

function THEMENAME_post_nav() {
// Don\'t print empty markup if there\'s nowhere to navigate.
$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, \'\', true );
$next     = get_adjacent_post( false, \'\', false );

if ( ! $next && ! $previous ) {
    return;
}
?>
<nav class="navigation post-navigation" role="navigation">
    <!-- <h1 class="screen-reader-text"><?php _e( \'Post navigation\', \'THEMENAME\' ); ?></h1> -->
    <div class="nav-links">
        <?php
            previous_post_link( \'<div class="nav-previous">\' . get_the_post_thumbnail() . \'%link</div>\', _x( \'%title\', \'Previous post link\', \'THEMENAME\' ) );
            next_post_link(     \'<div class="nav-next">\' . get_the_post_thumbnail() . \'%link</div>\',  _x( \'%title\', \'Next post link\',     \'THEMENAME\' ) );
        ?>
    </div><!-- .nav-links -->
</nav><!-- .navigation -->
<?php
}

SO网友:toni_lehtimaki

 previous_post_link( \'<div class="nav-previous">%link</div>$prevthumbnail\', _x( \'<span class="meta-nav">&larr;</span> %title\', \'Previous post link\', \'THEMENAME\' ) );
 next_post_link( \'<div class="nav-next">%link</div>$nextthumbnail\', _x( \'%title   <span class="meta-nav">&rarr;</span>\', \'Next post link\', \'THEMENAME\' );
在我看来,你似乎把变量$prevthumbnail$nexxtthumbnail 在单引号内。

按照我刚才所说的,我将尝试以下做法:

previous_post_link( \'<div class="nav-previous">%link</div>\'. $prevthumbnail, _x( \'<span class="meta-nav">&larr;</span> %title\', \'Previous post link\', \'THEMENAME\' ) );
next_post_link( \'<div class="nav-next">%link</div>\' . $nextthumbnail, _x( \'%title   <span class="meta-nav">&rarr;</span>\', \'Next post link\', \'THEMENAME\' );

结束

相关推荐

Wp_Query和Have_Posts的结果如此奇怪

我测试了一些简单的代码,但结果太奇怪了。Test 1:$featured_posts = new WP_Query( $query_args ); while ($featured_posts->have_posts()) { $featured_posts->the_post(); the_title(); echo \'<br>\'; } echo \'End: \"\' . $featured_po