본문 바로가기
워드프레스/워드프레스 팁

워드프레스 동일 카테고리 내에서 이동

by 도라미도라미도라미 2020. 8. 6.
같은 카테고리 글끼리만 이동가동하도록 하려면?

워드프레스는 글(POST,BLOG,PORTFOLIO)을 여러개 작성하면 이전글/ 다음글로 이동이 가능한 링크가 생성됩니다. (이동버튼이 나오게 안나오게 하는 방법은 테마에 따라 설정이 다를 수 있습니다. ) 그런데 이 글들을 카테고리 별로 묶어서 같은 카테고리끼리만 이동이 가능하도록 바꾸고 싶어서 이것저것 테스트를 해봤습니다. 그러다가 찾은 방법입니다. 크게 2가지 방법이 있습니다. 

 

01. function에 php 코드 삽입

// 이전 글/다음 글 내비게이션을 카테고리로 제한
// Restrict the post navigation to the same 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 );
}
// Source: https://presscustomizr.com/

검색하는 많이 나오는 코드입니다. 

사용방법은 

테마디자인 > 테마편집기 > function.php 파일을 열어 위에 코드를 넣어주면 됩니다. 

 

02. 워드프레스 테마 코드 수정하기

- 해당 php 파일 찾기 

post / page / portfolio 등 글을 올릴 수 있는 글페이지 레이아웃이 올려져 있는 php파일을 찾습니다. 

테마디자인 > 테마편집기 > single.php 파일 열기 

( 파일명은 테마마다 상의합니다.)

 

- 이전글/다음글 네이비게이션 액션에 대한 코드를 찾아 수정해줍니다.

previous_post_link( string $format = '« %link', string $link = '%title',
bool $in_same_term = false, array|string $excluded_terms = '', string $taxonomy = 'category' )
next_post_link( string $format = '« %link', string $link = '%title',
bool $in_same_term = false, array|string $excluded_terms = '', string $taxonomy = 'category' )

$format : (string) / (선택사항) / 링크 앵커 형식 (?) 이건 뭐지..? 

기본값 : "« %link"

$link : (string) / (선택사항) / 고유 링크 형식으로 연결

기본값 : "%title"

$in_same_term : (bool) / (선택사항) / 링크가 동일한 분류에 있어야 하는지 여부

기본값 : false

$excluded_terms : (array/ string) (선택사항) / 제외 id의 배열 또는 목록

기본값 : ""

$taxonomy : (string) / (선택사항) $in_same_term이 true면 

기본값 : 'category'

 

요기서 찾고자 했던건 "$in_same_term"입니다. 같은 분류에서만 이동가능 하게 해주는 속성인데, 기본값이 false입니다. 이부분을 찾아서 true로 바꿔주면 같은 분류끼리만 이동하도록 바뀝니다.

$taxonomy를 설정하면 '카테고리'를 다른 분류로 변경하여 그 분류내에서만 이동하도록 할 수 있습니다. 또한 $exculded_trems를 설정하면 특정 글만 제외시킬 수 있습니다. 

 

참고 및 출처 : https://developer.wordpress.org/reference/functions/previous_post_link/

 

WordPress Developer Resources | Official WordPress Developer Resources

Official WordPress developer resources including a code reference, handbooks (for APIs, plugin and theme development, block editor), and more.

developer.wordpress.org


둘 중에 하나를 사용하면 되는데, 저는 2번으로 했지만 저부분이 어디인지 찾는데 조금 오래걸렸습니다. 1번은 그냥 파일에 붙여넣기만 하면 되니 더 편리할 수 있을 것 같습니다. 그치만 좀더 세부적으로 조정을 하고 싶을땐 2번 방법을 사용하면 좋을 것 같아요! 

댓글