Try this Solution:
<为此,您应该创建自定义短代码。请在主题或子主题的函数中添加此代码。php文件:
add_shortcode( \'next_post\', \'next_shortcode\' );
function next_shortcode($atts) {
global $post;
ob_start();
next_post_link( \'<span class="nav-next">%link : Next ></span>\', \'%title\' );
$result = ob_get_contents();
ob_end_clean();
return $result;
}
add_shortcode( \'prev_post\', \'prev_shortcode\' );
function prev_shortcode($atts) {
global $post;
ob_start();
previous_post_link( \'<span class="nav-previous">< Previous : %link</span>\', \'%title\' );
$result = ob_get_contents();
ob_end_clean();
return $result;
}
function my_custom_adjacent_post_where($sql) {
if ( !is_main_query() || !is_singular() || \'your-custom-post-type-slug\' != get_post_type() )
return $sql;
$the_post = get_post( get_the_ID() );
$patterns = array();
$patterns[] = \'/post_date/\';
$patterns[] = \'/\\\'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\\\'/\';
$replacements = array();
$replacements[] = \'post_title\';
$replacements[] = $the_post->post_title;
return preg_replace( $patterns, $replacements, $sql );
}
add_filter( \'get_next_post_where\', \'my_custom_adjacent_post_where\' );
add_filter( \'get_previous_post_where\', \'my_custom_adjacent_post_where\' );
function my_custom_adjacent_post_sort($sql) {
if ( !is_main_query() || !is_singular() || \'your-custom-post-type-slug\' != get_post_type() )
return $sql;
$pattern = \'/post_date/\';
$replacement = \'post_title\';
return preg_replace( $pattern, $replacement, $sql );
}
add_filter( \'get_next_post_sort\', \'my_custom_adjacent_post_sort\' );
add_filter( \'get_previous_post_sort\', \'my_custom_adjacent_post_sort\' );
Whereas \'your-custom-post-type-slug\' is your custom post type slug, that you need to replace in the above code.
<然后,在内容模板中添加此短代码以显示上一篇和下一篇文章的链接:
[prev_post] | [next_post]
Expected Output:
< Previous : Post Name | Post Name : Next >