仅返回定制帖子的前两个条款

时间:2017-10-11 作者:jordan

嘿,我想知道我如何才能只显示与自定义帖子类型相关的两个术语。我试过使用$wpdb 但这对我不起作用,因为我也需要术语链接。有什么建议吗?

谢谢

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

也许这会有帮助。首先,获得所有职位的条件;然后,使用foreach 循环以检索和回显每个链接。最后一行代码将把循环限制在前两项。

    $terms = wp_get_post_terms(); // If you\'re not in the loop, you should pass the post\'s ID as an argument.

    $i = 0;

    foreach ( $terms as $term ) {

        $name = $term->name;
        $href = get_term_link( $term->term_id ); 

        echo \'<a href="\' . $href . \'">\' . $name . \'</a>\';

        if ( ++$i == 2 ) break; // Limit to first 2 tags

    }

SO网友:Jaed Mosharraf

可以使用此默认函数获得

$args = array(
    \'orderby\'           => \'name\', 
     \'order\'             => \'ASC\',
     \'hide_empty\'        => true, 
     \'exclude\'           => array(), 
     \'exclude_tree\'      => array(), 
     \'include\'           => array(),
     \'number\'            => \'\', 
     \'fields\'            => \'all\', 
     \'slug\'              => \'\',
     \'parent\'            => \'\',
     \'hierarchical\'      => true, 
     \'child_of\'          => 0,
     \'childless\'         => false,
     \'get\'               => \'\', 
     \'name__like\'        => \'\',
     \'description__like\' => \'\',
     \'pad_counts\'        => false, 
     \'offset\'            => \'\', 
     \'search\'            => \'\', 
     \'cache_domain\'      => \'core\'
 ); 
 $terms = get_terms($taxonomies, $args);
有关此链接的更多详细信息:https://codex.wordpress.org/es:Function_Reference/get_terms

结束