【问题标题】:Wordpress using get_term to retrieve slug not working as expectedWordpress 使用 get_term 检索 slug 未按预期工作
【发布时间】:2016-09-24 08:22:54
【问题描述】:

我正在使用下面的代码来尝试获取当前类别和父类别的 slug。

我已经设法获得了当前的 cat slug,但父级以可读的文本和 nut slug 格式显示。

我哪里错了?

    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
    $parent = get_term($term->parent, get_query_var('taxonomy') );?>

        <?php echo do_shortcode("[ecs-list-events cat='{$term->slug}']"); ?>   

     <?php 
    echo $term->slug; 
    echo $parent->name;
    ?>

【问题讨论】:

    标签: php wordpress taxonomy slug


    【解决方案1】:

    我最近自己设置了类似的东西。这是我用来完成类似操作的代码:

    <?php 
        global $post;    
        $terms = get_the_terms($post->id, 'my-custom-taxonomy-name');   
        $term = get_term_by( 'id', $terms[0]->term_id, 'my-custom-taxonomy-name');
        $parent = get_term($term->parent, 'my-custom-taxonomy-name' );
    
        echo $parent->slug; //This will return the parent slug
    ?>
    

    使用您的代码,您可以这样做:

    <?php 
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
    $parent = get_term($term->parent, get_query_var('taxonomy') );
    ?>
    
    <?php echo do_shortcode("[ecs-list-events cat='{$term->slug}']"); ?>   
    
    <?php 
    echo $term->slug; 
    echo $parent->slug; //change this to "slug"
    ?>
    

    您可能只需要更改“echo $parent->name;”到“回声 $parent->slug;”。此外,您应该查看这些文章,了解get_term_by()get_term() 函数可以返回哪些参数。

    【讨论】:

      猜你喜欢
      • 2012-10-07
      • 1970-01-01
      • 2018-12-23
      • 2021-10-13
      • 2017-05-12
      • 2014-05-29
      • 2018-10-17
      • 1970-01-01
      • 2015-03-09
      相关资源
      最近更新 更多