Em muitos sites e blogs WordPress, ao acessar uma postagem, é possível navegar para o post anterior ou o próximo utilizando um botão que é adicionado no final do conteúdo, como na imagem abaixo:

É possível restringir essa navegação entre posts, de forma que o usuário possa navegar somente para posts da mesma categoria.

Insira o código abaixo em seu site utilizando o plugin Code Snippets. Caso algo de errado, você pode desfazer essa alteração seguindo o passo a passo neste link.

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 );
}
0 respostas

Deixe uma resposta

Want to join the discussion?
Feel free to contribute!

Deixe um comentário

O seu endereço de e-mail não será publicado.