因此,为了做到这一点,我必须过滤SQL查询。我发现this snippet 按标题过滤帖子顺序,并为下一个和上一个帖子链接返回它们。不幸的是,这在自定义帖子类型中不起作用。所以我用get_post_type($post)
获取当前帖子类型并返回它。这样就可以在自定义帖子类型中使用。而且,如果您愿意,可以限制它,使其仅适用于自定义帖子类型,而不适用于博客帖子。我在这里没有这么做,但这是可能的。这是我在函数中输入的代码。php:
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\');
接下来,我设置了几个短代码,以便能够添加下一个和上一个帖子链接。因为如果没有返回任何内容,我使用的主题就会变得扭曲(第一篇和最后一篇文章不会分别为上一篇和下一篇文章链接返回任何内容),所以我需要返回一个空格:
function next_shortcode($atts) {
global $post;
ob_start();
next_post_link( \'<div class="nav-next">%link</div>\', \'Next Work <span class="arrow_carrot-right_alt2"></span>\' );
$result = ob_get_contents();
ob_end_clean();
$result = (!$result ? \'<div class="nav-next"> </div>\' : $result);
return $result;
}
function prev_shortcode($atts) {
global $post;
ob_start();
previous_post_link( \'<div class="nav-previous">%link</div>\', \'<span class="arrow_carrot-left_alt2"></span> Previous Work\' );
$result = ob_get_contents();
ob_end_clean();
$result = (!$result ? \'<div class="nav-previous"> </div>\' : $result);
return $result;
}
add_shortcode( \'prev_work\', \'prev_shortcode\' );
add_shortcode( \'next_work\', \'next_shortcode\' );
请注意,我使用的是优雅主题的优雅图标字体,所以
<span>
带箭头\\u carrot-left/right\\u alt2类用于。
无论如何,希望这对其他人有帮助!!