블로그라면 응당 포스트 마지막에 이전 글/다음 글로 이동하는 버튼이 있어야 한다. 대부분의 경우 사용중인 테마의 설정을 잘 찾아보면 이전글/다음글 이동 버튼을 추가할 수 있다. 또는 Pagination 문서를 참고해 직접 추가해 줄 수도 있다.

neve theme에는 "Post navigation"이라는 이름으로 구현되어 있다.
하지만 이렇게 삽입된 pagination 기능은 카테고리에 상관없이 포스트가 작성된 순서대로 이전/다음 글로 이동하는 경우가 많다. 워드프레스 관련 글을 읽던 사람이 다음글 버튼을 클릭했는데 갑자기 여행 포스트가 뜬다면 당황할 것이다. 이전 글/다음 글 버튼이 같은 카테고리 내의 포스트만 엮도록 수정하려면 functions.php 파일에 아래 코드를 추가하면 된다. (참고: 워드프레스 자식 테마 (Child Theme) 만드는 법)
# previous/next post button for posts in only same categories
# see: https://www.thewordcracker.com/intermediate/next-and-prev-post-link-only-within-category/
# =========================================================================================================
add_filter( 'get_next_post_join', 'navigate_in_same_taxonomy_join', 20);
add_filter( 'get_previous_post_join', 'navigate_in_same_taxonomy_join', 20 );
function navigate_in_same_taxonomy_join() {
global $wpdb;
return " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
}
add_filter( 'get_next_post_where' , 'navigate_in_same_taxonomy_where' );
add_filter( 'get_previous_post_where' , 'navigate_in_same_taxonomy_where' );
function navigate_in_same_taxonomy_where( $original ) {
global $wpdb, $post;
$where = '';
$taxonomy = 'category';
$op = ('get_previous_post_where' == current_filter()) ? '<' : '>';
$where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
return $original ;
$term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
$term_array = array_map( 'intval', $term_array );
if ( ! $term_array || is_wp_error( $term_array ) )
return $original ;
$where = " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
return $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $post->post_date, $post->post_type );
}
이제 이전 글/다음 글 버튼을 클릭하면 같은 카테고리 내의 포스트끼리만 이동이 될 것이다.
위의 이미지는 2020.02.09에 작성된 GCP에 LEMP stack + WordPress 설치하기 포스트의 이전 글/다음 글 버튼이다. 포스트 작성 날짜 순서로는 이전 글 버튼에 여행 카테고리의 [캐나다/옐로나이프] 오로라 여행 (4) 옐로나이프 4일 차 포스트가 링크되어야 하지만 functions.php에 코드를 추가한 결과 같은 워드프레스 카테고리의 글이 링크된 것을 볼 수 있다.
끝.