有些内置函数没有适当的过滤器来修改其输出,这有时令人恼火。get_boundary_post() 是内置功能之一。不幸的是,它没有根据帖子类型获取帖子,只有分类法。但是,您仍然可以使用此函数获取第一篇和最后一篇文章。它确实支持$in_same_term 参数,与next_post_link() 和previous_post_link() 功能。
这里只是一个注释,抄本页get_boundary_post() 不正确,有4个参数,如下(检查the source, 目前生产线1694-1750英寸wp-includes/link-template.php)
get_boundary_post( $in_same_term = false, $excluded_terms = \'\', $start = true, $taxonomy = \'category\' )
 如果需要对该函数进行更多控制,只需将完整函数复制到函数即可。php,重命名它并进行所需的修改
使用get_boundary_post() 如下所示,从名为mytax
//First post
$first = get_boundary_post( true, \'\', true, \'mytax\' );
echo apply_filters( \'the_title\', $first[0]->post_title );
//Last post
$last = get_boundary_post(true, \'\', false, \'mytax\');
echo apply_filters( \'the_title\', $last[0]->post_title );
 只要一个音符,如果你设置
$in_same_term 参数为true时,如果分类法不是默认分类法,则还需要设置分类法
category 例如,分类法
previous_post_link( \'%link\', \'Previous post in same term\', TRUE, \' \', \'mytax\' );
 编辑我想你得到同样帖子的原因很可能是
get_boundary_post 使用
get_posts 其中使用
WP_Query 默认为post类型
post. 正如我所说,这里没有过滤器。
最好是复制函数,重命名它,并根据需要进行修改。
试试这样的
function get_my_custom_boundary_post( $in_same_term = false, $excluded_terms = \'\', $start = true, $taxonomy = \'category\' ) {
    $post = get_post();
    if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
        return null;
    $query_args = array(
        \'post_type\' => \'MYPOSTTYPE\',
        \'posts_per_page\' => 1,
        \'order\' => $start ? \'ASC\' : \'DESC\',
        \'update_post_term_cache\' => false,
        \'update_post_meta_cache\' => false
    );
    $term_array = array();
    if ( ! is_array( $excluded_terms ) ) {
        if ( ! empty( $excluded_terms ) )
            $excluded_terms = explode( \',\', $excluded_terms );
        else
            $excluded_terms = array();
    }
    if ( $in_same_term || ! empty( $excluded_terms ) ) {
        if ( $in_same_term )
            $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( \'fields\' => \'ids\' ) );
        if ( ! empty( $excluded_terms ) ) {
            $excluded_terms = array_map( \'intval\', $excluded_terms );
            $excluded_terms = array_diff( $excluded_terms, $term_array );
            $inverse_terms = array();
            foreach ( $excluded_terms as $excluded_term )
                $inverse_terms[] = $excluded_term * -1;
            $excluded_terms = $inverse_terms;
        }
        $query_args[ \'tax_query\' ] = array( array(
            \'taxonomy\' => $taxonomy,
            \'terms\' => array_merge( $term_array, $excluded_terms )
        ) );
    }
    return get_posts( $query_args );
}
 那就换衣服吧
MYPOSTTYPE 并将之前给定的代码更改为
//First post
$first = get_my_custom_boundary_post( true, \'\', true, \'mytax\' );
echo apply_filters( \'the_title\', $first[0]->post_title );
//Last post
$last = get_my_custom_boundary_post(true, \'\', false, \'mytax\');
echo apply_filters( \'the_title\', $last[0]->post_title );