我知道这很古老,但我自己一直在寻找答案,但什么都找不到,所以我想与大家分享。事实上,我最近不得不自己处理一个非常类似的设置。WordPress内置的相邻帖子链接的问题在于(正如你所提到的),如果你的帖子碰巧是多个词,那么下一个和上一个链接通常会跳转到其他类别中的一个,而不是停留在你想要的类别中。以下是我为解决此问题所做的:
第一部分是一个返回ID的便捷函数:
function ss_get_post_ids($term, $taxonomy)
{
return get_posts(array(
\'post_type\' => \'project\', // if you are using a CPT, put it here.
\'numberposts\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => $taxonomy,
\'field\' => \'id\',
\'terms\' => is_array($term) ? $term : array($term),
),
),
\'fields\' => \'ids\',
));
}
接下来,我们将其命名为所需的术语和分类法:
$mytermid = \'5\'; // put your current term_id here
$mytaxonomy = \'project-type\'; // put your current taxonomy slug here
$relatedposts = ss_get_post_ids($mytermid, $mytaxonomy); // call our function above to get all the ids for this pair
global $post;
$currentpostID = $post->ID; // get current post ID
$currentKey = array_search($currentpostID, $relatedposts); //find current ID in our returned list of IDs to use it as base
// below get the previous and next IDs, and loop back to beginning if at end
$before = (isset($relatedposts[$currentKey - 1])) ? $relatedposts[$currentKey - 1] : $relatedposts[count($relatedposts) - 1];
$after = (isset($relatedposts[$currentKey + 1])) ? $relatedposts[$currentKey + 1] : $relatedposts[0];
// get the previous and next permalinks using the IDs above
$term_previous_post_link = get_permalink($before);
$term_next_post_link = get_permalink($after);
现在你可以使用
$term_previous_post_link
和
$term_next_post_link
取代WordPress自己的下一个和上一个帖子链接,它实际上将保持在同一期限内,即使有倍数。希望这对其他人有帮助,我花了很长时间努力寻找解决方案。