默认情况下the_post_navigation()
使用当前的帖子类型,无论它是什么。幸运的是,该函数最终调用get_adjacent_post()
有几个钩子我们可以用。以下使用get_{$adjacent}_post_where
哪里$adjacent
是“或”;“上一页”;或“或”;下一步:
/**
* Modify the posts navigation WHERE clause
* to include our acceptable post types
*
* @param String $where - Generated WHERE SQL Clause
*
* @return String
*/
function wpse375885_postnav_where( $where ) {
global $wpdb, $post;
// Return Early
if( empty( $post ) ) {
return $where;
}
$search = sprintf( "p.post_type = \'%s\'", $post->post_type );
// Return Early - $where already modified
if( false == strpos( $where, $search ) ) {
return $where;
}
// Almost all non-builtin in post types
$acceptable_post_types = array_merge(
array(
\'post\',
\'page\'
),
get_post_types( array(
\'_builtin\' => false
) )
);
$placeholders = array_fill( 0, count( $post_types ), \'%s\' );
$format = implode( \',\', $placeholders );
$replace = $wpdb->prepare(
"p.post_type IN ($format)",
$post_types
);
return str_replace( $search, $replace, $where );
}
add_filter( \'get_next_post_where\', \'wpse375885_postnav_where\' );
add_filter( \'get_previous_post_where\', \'wpse375885_postnav_where\' );
就我个人而言,我会取代
$acceptable_post_types
使用我已知的帖子类型数组,以防止将来安装的插件被添加到列表中
您也可以更改
\'_builtin\' => true
真正引入所有岗位类型。