サポート » 使い方全般 » previous_post_link、next_post_linkのリンク先がおかしい

  • ブログの各記事の最下部に「前の記事へ」「次の記事へ」を配置しているのですが、動作がおかしいです。
    idが1,2,3の記事があったとして、2の記事を開いたときのリンク先が、
    前の記事へ → 1
    次の記事へ → 2
    と「次の記事へ」のリンク先が自分になってしまいます。

    原因がわかりますでしょうか?

    以下、ソースコードです。
    ————————————-
    <!– 次へ前へ –>
    <div class=”blog_pager”>
    <?php the_date(); ?> 投稿|
    <?php previous_post_link(‘%link’,’« 前の記事へ’,TRUE); ?>
    <?php next_post_link(‘%link’,’次の記事へ »’,TRUE); ?>
    </div>

    • このトピックはtoebisutanakaが7年、 7ヶ月前に変更しました。
    • このトピックはtoebisutanakaが7年、 7ヶ月前に変更しました。
8件の返信を表示中 - 1 - 8件目 (全8件中)
  • toebisutanakaさん、こんにちは。
    previous_post_linkや、next_post_linkはID順じゃなくて、日付順になるかと思います。

    previous post link

    next_post_link

    各日付を見なおしてみると良いかもしれません。

    トピック投稿者 toebisutanaka

    (@toebisutanaka)

    ご回答ありがとうございます。
    そうですね、日付順のようです。
    ただ、投稿日は1→2→3の順なので、やはりリンク先が自分なのはおかしいです・・・。

    念のため、全ての記事が同じカテゴリーに所属しているという前提で大丈夫でしょうか?

    トピック投稿者 toebisutanaka

    (@toebisutanaka)

    2つのカテゴリーがあり、上記1,2,3はすべて同じカテゴリーに所属しています。

    テーマまたはプラグインが next_post_link() の内容に手を加えているかもしれませんね。お使いのテーマとプラグインは何でしょう。また、テーマを Twenty Sixteen にして、プラグインをすべて停止した場合はどうでしょう。

    • この返信は7年、 7ヶ月前にgblsmが編集しました。
    トピック投稿者 toebisutanaka

    (@toebisutanaka)

    gblsmさん、ご返信ありがとうございます。
    テーマは独自のテーマを使っており、その中で上記のコーディングを行っています。

    next_post_linkでgrep検索したところ、2ソースがヒットしました。
    ヒットしたソースを載せます。
    なにかわかりますでしょうか?

    wp-includes/deprecated.php
    ————————————
    /**
    * Prints link to the next post.
    *
    * @since 0.71
    * @deprecated 2.0.0 Use next_post_link()
    * @see next_post_link()
    *
    * @param string $format
    * @param string $next
    * @param string $title
    * @param string $in_same_cat
    * @param int $limitnext
    * @param string $excluded_categories
    */
    function next_post($format=’%’, $next=’next post: ‘, $title=’yes’, $in_same_cat=’no’, $limitnext=1, $excluded_categories=”) {
    _deprecated_function( __FUNCTION__, ‘2.0.0’, ‘next_post_link()’ );
    if ( empty($in_same_cat) || ‘no’ == $in_same_cat )
    $in_same_cat = false;
    else
    $in_same_cat = true;
    $post = get_next_post($in_same_cat, $excluded_categories);
    if ( !$post )
    return;
    $string = ‘ID).'”>’.$next;
    if ( ‘yes’ == $title )
    $string .= apply_filters(‘the_title’, $post->post_title, $post->ID);
    $string .= ‘
    ‘;
    $format = str_replace(‘%’, $string, $format);
    echo $format;
    }
    ————————————

    wp-includes/link-template.php
    ————————————
    /**
    * Retrieves the next post link that is adjacent to the current post.
    *
    * @since 3.7.0
    *
    * @param string $format Optional. Link anchor format. Default ‘« %link’.
    * @param string $link Optional. Link permalink format. Default ‘%title’.
    * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
    * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
    * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default ‘category’.
    * @return string The link URL of the next post in relation to the current post.
    */
    function get_next_post_link( $format = ‘%link »’, $link = ‘%title’, $in_same_term = false, $excluded_terms = ”, $taxonomy = ‘category’ ) {
    return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
    }

    /**
    * Displays the next post link that is adjacent to the current post.
    *
    * @since 1.5.0
    * @see get_next_post_link()
    *
    * @param string $format Optional. Link anchor format. Default ‘« %link’.
    * @param string $link Optional. Link permalink format. Default ‘%title’
    * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
    * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
    * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default ‘category’.
    */
    function next_post_link( $format = ‘%link »’, $link = ‘%title’, $in_same_term = false, $excluded_terms = ”, $taxonomy = ‘category’ ) {
    echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
    }

    /**
    * Retrieves the adjacent post link.
    *
    * Can be either next post link or previous.
    *
    * @since 3.7.0
    *
    * @param string $format Link anchor format.
    * @param string $link Link permalink format.
    * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
    * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs. Default empty.
    * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
    * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default ‘category’.
    * @return string The link URL of the previous or next post in relation to the current post.
    */
    function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = ”, $previous = true, $taxonomy = ‘category’ ) {
    if ( $previous && is_attachment() )
    $post = get_post( get_post()->post_parent );
    else
    $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
    if ( ! $post ) {
    $output = ”;
    } else {
    $title = $post->post_title;
    if ( empty( $post->post_title ) )
    $title = $previous ? __( ‘Previous Post’ ) : __( ‘Next Post’ );
    /** This filter is documented in wp-includes/post-template.php */
    $title = apply_filters( ‘the_title’, $title, $post->ID );
    $date = mysql2date( get_option( ‘date_format’ ), $post->post_date );
    $rel = $previous ? ‘prev’ : ‘next’;
    $string = ‘‘;
    $inlink = str_replace( ‘%title’, $title, $link );
    $inlink = str_replace( ‘%date’, $date, $inlink );
    $inlink = $string . $inlink . ‘
    ‘;
    $output = str_replace( ‘%link’, $inlink, $format );
    }
    $adjacent = $previous ? ‘previous’ : ‘next’;

    /**
    * Filters the adjacent post link.
    *
    * The dynamic portion of the hook name, $adjacent, refers to the type
    * of adjacency, ‘next’ or ‘previous’.
    *
    * @since 2.6.0
    * @since 4.2.0 Added the $adjacent parameter.
    *
    * @param string $output The adjacent post link.
    * @param string $format Link anchor format.
    * @param string $link Link permalink format.
    * @param WP_Post $post The adjacent post.
    * @param string $adjacent Whether the post is previous or next.
    */
    return apply_filters( “{$adjacent}_post_link”, $output, $format, $link, $post, $adjacent );
    }
    ————————————

    • この返信は7年、 7ヶ月前にtoebisutanakaが編集しました。

    載せられたのは WordPress コアのファイルですよね、そちらに問題があるかもしれませんが考えにくいです。
    いちどテーマを Twenty Sixteen などの別テーマに変えて検証してはいかがですか。

    トピック投稿者 toebisutanaka

    (@toebisutanaka)

    業者に委託して解決しました。
    解決後のソースを確認しましたが、よくわかりませんでした・・・。

    ソースをざっくり抜粋すると次のとおりです。

    【改修前】

    <?php the_title(); ?>
    <?php the_post(); ?>
    <?php the_content(); ?>
    <?php previous_post_link('%link','&laquo;&nbsp;前の記事へ',TRUE); ?>
    <?php next_post_link('%link','次の記事へ&nbsp;&raquo;',TRUE); ?>

    【改修後】

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php the_title(); ?>
    <?php the_content(); ?>
    <?php previous_post_link('%link','&laquo;&nbsp;前の記事へ',TRUE); ?>
    <?php next_post_link('%link','次の記事へ&nbsp;&raquo;',TRUE); ?>
    <?php endwhile;endif; ?>
8件の返信を表示中 - 1 - 8件目 (全8件中)
  • トピック「previous_post_link、next_post_linkのリンク先がおかしい」には新たに返信することはできません。