如何转到其他帖子与下一篇文章,但停留在同一类别
在WordPress中,你可以使用相关的插件或自定义代码来实现在同一类别中转到其他帖子和下一篇文章的功能。以下是一种实现方法:
function custom_previous_next_post_links($format, $link, $post, $adjacent){
$taxonomy = 'category'; // 设置为你希望使用的分类法名称
$term_list = get_the_terms($post->ID, $taxonomy);
$term_slug = $term_list[0]->slug; // 获取当前文章的分类法别名
$previous = '« 上一篇';
$next = '下一篇 »';
$previous_post = get_previous_post(true, '', $taxonomy);
$next_post = get_next_post(true, '', $taxonomy);
if ($adjacent) {
$post = $previous_post;
$link_text = $previous;
} else {
$post = $next_post;
$link_text = $next;
}
if ($post) {
$permalink = get_permalink($post->ID);
$title = get_the_title($post->ID);
$link = '<a href="' . $permalink . '">' . $title . '</a>';
$format = str_replace('%link', $link, $format);
$format = str_replace('%title', $title, $format);
$format = str_replace('%date', mysql2date(get_option('date_format'), $post->post_date), $format);
$format = str_replace('%previous', $previous, $format);
$format = str_replace('%next', $next, $format);
} else {
$format = '';
}
return $format;
}
// 用于上一篇文章导航
function custom_previous_post_link($format='« %link', $link='%title', $in_same_term = false, $taxonomy = 'category'){
return custom_previous_next_post_links($format, $link, $post, true);
}
// 用于下一篇文章导航
function custom_next_post_link($format='%link »', $link='%title', $in_same_term = false, $taxonomy = 'category'){
return custom_previous_next_post_links($format, $link, $post, false);
}
然后,在你的主题模板文件中,你可以使用以下代码来显示上一篇和下一篇文章的导航链接:
<?php
// 上一篇文章导航链接
echo custom_previous_post_link();
// 下一篇文章导航链接
echo custom_next_post_link();
?>
请注意,如果你的主题已经提供了这些导航链接的功能,你可能不需要添加上述自定义代码。
以上就是实现在同一类别中转到其他帖子和下一篇文章的方法。希望对你有所帮助!如果你对云计算、IT互联网领域的其他问题有兴趣,请随时提问。