問題 × 解決策

カスタム投稿で「前の記事・次の記事」【get_adjacent_post】を適用させる

問題

カスタム投稿で「前の記事・次の記事」【get_adjacent_post】を適用させるには?

解決策

  • functions.phpで、カスタム投稿を適用させる記述を行います。
  • 「mod_get_adjacent_post('prev または next', array($post_type, '', 'true'))」でテンプレートファイルに出力します。

 

カスタム投稿で「前の記事・次の記事」を適用する。

「前の記事・次の記事」を表示させる「get_adjacent_post」はデフォルトではカスタム投稿に対応していません。
そこで、カスタム投稿で「前の記事・次の記事」【get_adjacent_post】を適用させる方法をご紹介します。

functions.phpに記述を行う。

functions.phpにカスタム投稿を適用させる記述を紹介してくれているサイトを見つけました。
こちらをお借りします。

function mod_get_adjacent_post($direction = 'prev', $post_types = 'post') {
    global $post, $wpdb;
    if(empty($post)) return NULL;
    if(!$post_types) return NULL;
    if(is_array($post_types)){
        $txt = '';
        for($i = 0; $i <= count($post_types) - 1; $i++){
            $txt .= "'".$post_types[$i]."'";
            if($i != count($post_types) - 1) $txt .= ', ';
        }
        $post_types = $txt;
    }
    $current_post_date = $post->post_date;
    $join = '';
    $in_same_cat = FALSE;
    $excluded_categories = '';
    $adjacent = $direction == 'prev' ? 'previous' : 'next';
    $op = $direction == 'prev' ? '<' : '>';
    $order = $direction == 'prev' ? 'DESC' : 'ASC';
    $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
    $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type IN({$post_types}) AND p.post_status = 'publish'", $current_post_date), $in_same_cat, $excluded_categories );
    $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
    $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
    $query_key = 'adjacent_post_' . md5($query);
    $result = wp_cache_get($query_key, 'counts');
    if ( false !== $result )
        return $result;
    $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
    if ( null === $result )
        $result = '';
    wp_cache_set($query_key, $result, 'counts');
    return $result;
}

テンプレートファイルに出力します。

テンプレートファイルに出力します。
mod_get_adjacent_post(‘prev または next’, array($post_type, ”, ‘true’))」で出力することができます。

<?php
if(is_single()):
$prevpost = mod_get_adjacent_post('prev', array($post_type, '', 'true'));
$nextpost = mod_get_adjacent_post('next', array($post_type, '', 'true'));
if( $prevpost or $nextpost ){ //前の記事、次の記事いずれか存在しているとき
?>
<div class="prevnext-box clearfix section">
<?php
if ( $prevpost ) { //前の記事が存在しているとき
echo '<div class="prev prevnext"><a href="' . get_permalink($prevpost->ID) . '">' . $prevpost->post_title . '</a></div>';
}
if ( $nextpost ) { //次の記事が存在しているとき
echo '<div class="next prevnext"><a href="' . get_permalink($nextpost->ID) . '">' . $nextpost->post_title . '</a></div>';
}
?>
</div>
<?php
}
endif;
?>

参考ページ

以下のサイトが、より詳しく解説してくれています。

【wordpress – get_adjacent_post()をカスタムの投稿タイプで機能させる – ITツールウェブ】
https://ja.coder.work/so/wordpress/691774

コメント

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

5 × two =

「問題 × 解決策」
月別アーカイブ一覧

「問題 × 解決策」
月別アーカイブ一覧