【问题标题】:Get all category name of a taxonomy dinamically in wordpress在 wordpress 中动态获取分类的所有类别名称
【发布时间】:2019-05-04 12:37:26
【问题描述】:

我必须为朋友做一个项目,我需要知道是否可以在 wordpress 中动态检索分类的所有类别名称。如果可以,我该怎么办?

【问题讨论】:

  • 你已经在论坛上搜索了吗?因为像你这样的问题还有很多。
  • 我已经搜索过,但没有找到适合我的范围的具体答案
  • 观看此线程以获取更多信息:stackoverflow.com/q/55981213/4582744
  • 我不明白我必须在属性中添加什么:字段和术语。我不明白这两者之间的区别

标签: php wordpress add


【解决方案1】:

是的,你可以做到!首先在function.php文件末尾添加以下代码:

function current_cat() { 
    global $post;
    if ( is_page() && $post->post_parent )
     $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' . '&depth=1' );
      else
      $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' . '&depth=1' );
    if ( $childpages ) {
    $string = '<ul>' . $childpages . '</ul>';
    }
    return $string;
}
add_shortcode('currentcat', 'current_cat');

然后您几乎可以通过调用短代码名称在任何地方使用它:

 [currentcat]

【讨论】:

  • 谢谢,太好了!一个问题:是否可以使用 get_terms 来做这件事?
  • 没问题。对于这种情况,我没有尝试过。但我在这个网站上使用了这个简码,效果很好,你可以在帖子上方看到当前菜单的类别:teram-packaging.com/en/teram-group/social-responsibilities
  • 我想利用你的礼貌:):) 是否可以这样做:terms => all...用 tax_query 检索所有类别?
【解决方案2】:

只要使用这个函数get_terms()

$taxonomies = get_terms( array(
    'taxonomy' => 'taxonomy_name'
) );

if ( !empty($taxonomies) ) :
    foreach( $taxonomies as $category ) {
      print_r($category);
    }
endif;

更多参考https://developer.wordpress.org/reference/functions/get_terms/,本教程也可能对你有所帮助Show Custom Taxonomy

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 2015-11-09
    • 2017-09-20
    • 1970-01-01
    相关资源
    最近更新 更多