可以获得上一次/下一次分类术语存档URL吗?

时间:2016-04-03 作者:user3137901

我有一个自定义的帖子类型,叫做shows 和分类法podcast. 使用taxonomy-podcast.php, 我很难找到将生成下一个/上一个术语URL存档的函数。

例如:

URL:URL。com/podcast/example-term-2

Example Term 2

岗位1、岗位2、岗位3

所需输出

< Previous Term(url.com/podcast/example-term-1)    .   . . . . | . . . . .     Next Term(url.com/podcast/example-term-3)>

2 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

这是很有可能实现的。我们需要做的是

获取与我们的分类法相关的所有术语,按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 参数,它接受三个值,一个空字符串,previousnext

以下是您可以做的一个示例。您可以在此进行调整以满足您的需要

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\' ); 仅显示下一个术语

SO网友:majick

像这样的东西可以做到,如果你有很多术语,这可能是一个相当昂贵的查询,虽然它检索了所有术语,但目前无法找到解决方法。。。(虽然您可以在瞬间缓存所有术语结果,但可能不是每次页面加载都会这样做。)

function custom_term_navigation() {
    $thistermid = get_queried_object()->term_id;
    if (!is_numeric($thistermid)) {return;}

    // remove commenting to use transient storage
    // $terms = get_transient(\'all_podcast_terms\');
    // if (!$terms) {
        $args = array(\'orderby\'=>\'slug\');
        $terms = get_terms(\'podcast\',$args);
    //  set_transient(\'all_podcast_terms\',$terms,60*60);
    // }

    $termid = \'\'; $termslug = \'\'; $foundterm = false;
    foreach ($terms as $term) {
        $prevterm = $termslug;
        $termid = $term->term_id;
        if ($foundterm) {$nextterm = $term->slug; break;}
        if ($termid == $thistermid) {
            $foundterm = true; $previousterm = $prevterm;
        }
        $termslug = $term->slug;
    }

    $navigation = \'\';
    if ($previousterm != \'\') {
        $previoustermlink = get_term_link($previousterm,\'podcast\');
        $navigation .= "".$previoustermlink."\' style=\'float:left; display:inline;\'>&larr; Previous Term</a>";
    }
    if ($nexterm != \'\') {
        $nexttermlink = get_term_link($nexterm,\'podcast\');
        $navigation .= "<a href=\'".$nextermlink."\' style=\'float:right; display:inline;\'>Next Term &rarr;</a>";
    }

    return $navigation;
}

相关推荐

ACF Taxonomy in Loop

你好吗我的问题是,我在头版上显示了一些卡片,例如名称和描述,这些信息来自我的分类法event,因为我用ACF创建了一些字段,我想在卡片中打印它们,但我不知道怎么做。下面是我如何打印卡片的代码 <?php if(is_front_page()): $terms = get_terms( [ \'taxonomy\' => \'evento\', \'hide_empty\' =>