大家在使用Kadence主题中的elements创建模版的时候,需要用到上一页或者下一页功能,但是实际使用中,发现上一篇文章或者下一篇文章会跳到其他目录中。
elements我已经设置好了上一页或者下一页功能,如下图:

当我进入到页面中测试上一页下一页的时候,会出现跳到其他目录中,如下图:

如上图所示,我这篇文章明明是在Facebook基础教程目录中,上一篇是没有的,但是会跳到我独立站建站的目录中了,这个是Kadence设计逻辑引起的。
解决办法:
/*上一页下一页*/
add_filter( 'get_previous_post_join', 'limit_prev_next_same_category_join', 10, 5 );
add_filter( 'get_next_post_join', 'limit_prev_next_same_category_join', 10, 5 );
add_filter( 'get_previous_post_where', 'limit_prev_next_same_category_where', 10, 5 );
add_filter( 'get_next_post_where', 'limit_prev_next_same_category_where', 10, 5 );
function limit_prev_next_same_category_join( $join, $in_same_term, $excluded_terms, $taxonomy, $post ) {
global $wpdb;
if ( 'category' === $taxonomy ) {
$join .= " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id
INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
}
return $join;
}
function limit_prev_next_same_category_where( $where, $in_same_term, $excluded_terms, $taxonomy, $post ) {
global $wpdb;
if ( 'category' === $taxonomy ) {
$categories = wp_get_post_categories( $post->ID );
if ( ! empty( $categories ) ) {
$where .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode( ',', $categories ) . ")";
}
}
return $where;
}
把上边的代码复制到 Kadence 的 function.php中,如下图:

更新文件即可,这样前台就会调取当前类目的上一页下一页的文章了,是不是很简单呢?
