web-dev-qa-db-ja.com

Underscores.me post_nav関数で次/前の投稿のサムネイルを取得する

プロジェクトにUnderscores.meを使用しています。注目の画像(サムネイル)を前/次の投稿リンクに追加したいです。それらは私が編集しようとしている関数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
mtwallet

変数$prevthumbnail$nextthumbnailの代わりにget_the_post_thumbnail()を使うことで問題を解決しました。

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

}

1
mtwallet
 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' );
0
toni_lehtimaki