下一篇/上一篇文章链接:按字母顺序排列并来自同一类别

时间:2015-10-01 作者:Dormiu

我正在竭尽全力地寻找一种方法Next and Previous Post Links 以不同的方式出现在Single Post.

By DEFAULT it:

    按时间顺序排列

    所有博客类别的帖子链接

But I NEED it:

<按字母顺序排列

仅链接到同一类别的帖子

我不是开发人员,但我想如果我能合并下面的两个代码,问题就会解决。

CODE 1 - 按字母顺序打开下一个/上一个链接,但不是来自同一类别(source)

function filter_next_post_sort($sort) {
    $sort = "ORDER BY p.post_title ASC LIMIT 1";
    return $sort;
}
function filter_next_post_where($where) {
    global $post, $wpdb;
    return $wpdb->prepare("WHERE p.post_title > \'%s\' AND p.post_type = \'". get_post_type($post)."\' AND p.post_status = \'publish\'",$post->post_title);
}

function filter_previous_post_sort($sort) {
    $sort = "ORDER BY p.post_title DESC LIMIT 1";
    return $sort;
}
function filter_previous_post_where($where) {
    global $post, $wpdb;
    return $wpdb->prepare("WHERE p.post_title < \'%s\' AND p.post_type = \'". get_post_type($post)."\' AND p.post_status = \'publish\'",$post->post_title);
}

add_filter(\'get_next_post_sort\',   \'filter_next_post_sort\');
add_filter(\'get_next_post_where\',  \'filter_next_post_where\');

add_filter(\'get_previous_post_sort\',  \'filter_previous_post_sort\');
add_filter(\'get_previous_post_where\', \'filter_previous_post_where\');
CODE 2 - 从同一类别转到下一个/上一个链接,但不按字母顺序(source)

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

THANK YOU FOR HELP ME!

2 个回复
最合适的回答,由SO网友:cybmeta 整理而成

要获得下一个/上一个帖子,通常你只需要true 在get next/prev post函数中,该参数指示next/prev post应该位于相同的分类术语中,您不需要处理任何筛选器。

例如:

要获取next和prev post对象,请执行以下操作:

// First parameter for these functions indicates
// if next/prev post should be in the same taxonomy term
// More in https://codex.wordpress.org/Function_Reference/get_next_post
// And https://codex.wordpress.org/Function_Reference/get_previous_post
$prev_post = get_previous_post(true);
$next_post = get_next_post(true);
要打印上一篇/下一篇文章链接:

// Third parameter for these functions indicates
// if next/prev post should be in the same taxonomy term
// More in https://codex.wordpress.org/Template_Tags/next_post_link
// And https://codex.wordpress.org/Template_Tags/previous_post_link
previous_post_link( \'&laquo; %link\', \'%title\', true );
next_post_link( \'%link &raquo;\', \'%title\', true );
问题是,当您过滤where语句以更改顺序时,您会更改相同的分类部分,因此需要重新构建它。

这段代码应该可以工作(我还没有测试):

add_filter(\'get_next_post_sort\',  \'filter_next_and_prev_post_sort\');
add_filter(\'get_previous_post_sort\',  \'filter_next_and_prev_post_sort\');
function filter_next_and_prev_post_sort($sort) {
    $op = (\'get_previous_post_sort\' == current_filter()) ? \'DESC\' : \'ASC\';
    $sort = "ORDER BY p.post_title ".$op ." LIMIT 1";
    return $sort;

}

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\' , \'filter_next_and_prev_post_where\' );
add_filter( \'get_previous_post_where\' , \'filter_next_and_prev_post_where\' );
function filter_next_and_prev_post_where( $original ) {
  global $wpdb, $post;
  $where = \'\';
  $taxonomy   = \'category\';
  $op = (\'get_previous_post_where\' == current_filter()) ? \'<\' : \'>\';

  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_title $op %s AND p.post_type = %s AND p.post_status = \'publish\' $where", $post->post_title, $post->post_type );
}

SO网友:piskedi

适用于自定义帖子类型类别,工作完美无瑕。

我的问题是只在某些页面上运行此筛选器。如何添加一个。

如果(!is\\u singular(\'portfolio\'),则添加

add_filter( \'get_next_post_join\', \'results_navigate_in_same_taxonomy_join\', 20);
add_filter( \'get_previous_post_join\', \'results_navigate_in_same_taxonomy_join\', 20 );
function results_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\' , \'results_navigate_in_same_taxonomy_where\' );
add_filter( \'get_previous_post_where\' , \'results_navigate_in_same_taxonomy_where\' );
function results_navigate_in_same_taxonomy_where( $original ) {
    global $wpdb, $post;
    $where      = \'\';
    $taxonomy   = \'portfolio_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 );
}

相关推荐