How to Display a List of Parent Categories (with L

2019-05-21 19:43发布

So i have this dilemma on displaying a list of all the parent categories in wordpress.

I'm using this code below:

<ul><li><?php wp_list_categories('orderby=order&title_li=&depth=1'); ?></li></ul>

Unfortunately, this doesn't work which I really don't know why. what happens is it only shows the only ONE category.

I would appreciate anyone's help on this please.

标签: php wordpress
1条回答
聊天终结者
2楼-- · 2019-05-21 20:14

Try this one. I have tested before

<?php 
    $args = array(
      'orderby' => 'name',
      'hierarchical' => 1,
      'taxonomy' => 'category',
      'hide_empty' => 0,
      'parent' => 0,
    );
    $categories = get_categories($args);

    foreach($categories as $category) {

      echo '<a href="' . get_category_link($category->cat_ID) . '" title="' . $category->name . '">' . $category->name . '</a><br>';

      } 
  ?>

Or try this. Let me know which one is better.

<?php wp_list_cats('optionall=0&depth=1&list=0&exclude=1&children=0&hide_empty=0'); ?`>
查看更多
登录 后发表回答