这是很有可能实现的。我们需要做的是
获取与我们的分类法相关的所有术语,按slug(或任何其他所需字段)排序
获取当前术语对象
确定当前术语在数组中的位置
获取两个相邻的术语(如果有)
建立这些术语页面的链接
我编写了一个函数来处理这个问题。将业务逻辑排除在模板之外总是很重要的。模板应该只知道函数名,而不知道整个函数。这样,您就可以保持模板的干净性和可维护性
该函数至少需要PHP 5.4并包含基本内容,您可以根据自己的需要进行调整
function get_tax_navigation( $taxonomy = \'category\' )
{
// Make sure we are on a taxonomy term/category/tag archive page, if not, bail
if ( \'category\' === $taxonomy ) {
if ( !is_category() )
return false;
} elseif ( \'post_tag\' === $taxonomy ) {
if ( !is_tag() )
return false;
} else {
if ( !is_tax( $taxonomy ) )
return false;
}
// Make sure the taxonomy is valid and sanitize the taxonomy
if ( \'category\' !== $taxonomy
|| \'post_tag\' !== $taxonomy
) {
$taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
if ( !$taxonomy )
return false;
if ( !taxonomy_exists( $taxonomy ) )
return false;
}
// Get the current term object
$current_term = get_term( $GLOBALS[\'wp_the_query\']->get_queried_object() );
// Get all the terms ordered by slug
$terms = get_terms( $taxonomy, [\'orderby\' => \'slug\'] );
// Make sure we have terms before we continue
if ( !$terms )
return false;
// Because empty terms stuffs around with array keys, lets reset them
$terms = array_values( $terms );
// Lets get all the term id\'s from the array of term objects
$term_ids = wp_list_pluck( $terms, \'term_id\' );
/**
* We now need to locate the position of the current term amongs the $term_ids array. \\
* This way, we can now know which terms are adjacent to the current one
*/
$current_term_position = array_search( $current_term->term_id, $term_ids );
// Set default variables to hold the next and previous terms
$previous_term = \'\';
$next_term = \'\';
// Get the previous term
if ( 0 !== $current_term_position )
$previous_term = $terms[$current_term_position - 1];
// Get the next term
if ( intval( count( $term_ids ) - 1 ) !== $current_term_position )
$next_term = $terms[$current_term_position + 1];
$link = [];
// Build the links
if ( $previous_term )
$link[] = \'Previous Term: <a href="\' . esc_url( get_term_link( $previous_term ) ) . \'">\' . $previous_term->name . \'</a>\';
if ( $next_term )
$link[] = \'Next Term: <a href="\' . esc_url( get_term_link( $next_term ) ) . \'">\' . $next_term->name . \'</a>\';
return implode( \' ...|... \', $link );
}
用法现在您只需在需要的地方使用该功能,如下所示:
echo get_tax_navigation( \'podcast\' );
编辑-从评论来看,只有两个问题:(1)当我们上学期结束时,我们还能下学期吗:(第一学期的URL)反之亦然?基本上是一个无限项循环。(2) 出于主题化的目的,我如何使用此函数分别输出下一个和上一个URL?(例如get\\u tax\\u导航(\'podcast\',\'previous\')和get\\u tax\\u导航(\'podcast\',\'next\'))
当我们在第一学期的最后一学期时,我们所需要做的就是根据我们所处的位置来把握第一学期或最后一学期。显然,当我们在最后一个学期时,我们会抓住第一个学期,维卡也会抓住第一个学期
我们可以介绍$direction
参数,它接受三个值,一个空字符串,previous
或next
以下是您可以做的一个示例。您可以在此进行调整以满足您的需要
function get_tax_navigation( $taxonomy = \'category\', $direction = \'\' )
{
// Make sure we are on a taxonomy term/category/tag archive page, if not, bail
if ( \'category\' === $taxonomy ) {
if ( !is_category() )
return false;
} elseif ( \'post_tag\' === $taxonomy ) {
if ( !is_tag() )
return false;
} else {
if ( !is_tax( $taxonomy ) )
return false;
}
// Make sure the taxonomy is valid and sanitize the taxonomy
if ( \'category\' !== $taxonomy
|| \'post_tag\' !== $taxonomy
) {
$taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
if ( !$taxonomy )
return false;
if ( !taxonomy_exists( $taxonomy ) )
return false;
}
// Get the current term object
$current_term = get_term( $GLOBALS[\'wp_the_query\']->get_queried_object() );
// Get all the terms ordered by slug
$terms = get_terms( $taxonomy, [\'orderby\' => \'slug\'] );
// Make sure we have terms before we continue
if ( !$terms )
return false;
// Because empty terms stuffs around with array keys, lets reset them
$terms = array_values( $terms );
// Lets get all the term id\'s from the array of term objects
$term_ids = wp_list_pluck( $terms, \'term_id\' );
/**
* We now need to locate the position of the current term amongs the $term_ids array. \\
* This way, we can now know which terms are adjacent to the current one
*/
$current_term_position = array_search( $current_term->term_id, $term_ids );
// Set default variables to hold the next and previous terms
$previous_term = \'\';
$next_term = \'\';
// Get the previous term
if ( \'previous\' === $direction
|| !$direction
) {
if ( 0 === $current_term_position ) {
$previous_term = $terms[intval( count( $term_ids ) - 1 )];
} else {
$previous_term = $terms[$current_term_position - 1];
}
}
// Get the next term
if ( \'next\' === $direction
|| !$direction
) {
if ( intval( count( $term_ids ) - 1 ) === $current_term_position ) {
$next_term = $terms[0];
} else {
$next_term = $terms[$current_term_position + 1];
}
}
$link = [];
// Build the links
if ( $previous_term )
$link[] = \'Previous Term: <a href="\' . esc_url( get_term_link( $previous_term ) ) . \'">\' . $previous_term->name . \'</a>\';
if ( $next_term )
$link[] = \'Next Term: <a href="\' . esc_url( get_term_link( $next_term ) ) . \'">\' . $next_term->name . \'</a>\';
return implode( \' ...|... \', $link );
}
您可以用三种不同的方式使用代码
echo get_tax_navigation( \'podcast\' );
显示下一个和上一个术语echo get_tax_navigation( \'podcast\', \'previous\' );
仅显示上一个术语echo get_tax_navigation( \'podcast\', \'next\' );
仅显示下一个术语