最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to show a list of only categories (exclude subcategories)

programmeradmin2浏览0评论

I have a list where you show me the categories, but I would like you not to show me the subcategories, only the categories

 $term_id = get_categories();
    if (function_exists('get_wp_term_image'))
{
    $meta_image = get_wp_term_image($term_id); 
    //It will give category/term image url 
}
$categories = get_categories();
foreach($categories as $category) {
   ?>
  <article>
   <div class="poster"> 
   <a href="<?php echo get_category_link($category->term_id); ?>">
<?php echo '<img src="'.get_wp_term_image($category->term_id).'">' ?>
</div>
<div class="orbit_starts">
<h2><?php echo $category->name ?></h2></a>
</div>
</article>

How would you exclude all subcategories?

Thank you

I have a list where you show me the categories, but I would like you not to show me the subcategories, only the categories

 $term_id = get_categories();
    if (function_exists('get_wp_term_image'))
{
    $meta_image = get_wp_term_image($term_id); 
    //It will give category/term image url 
}
$categories = get_categories();
foreach($categories as $category) {
   ?>
  <article>
   <div class="poster"> 
   <a href="<?php echo get_category_link($category->term_id); ?>">
<?php echo '<img src="'.get_wp_term_image($category->term_id).'">' ?>
</div>
<div class="orbit_starts">
<h2><?php echo $category->name ?></h2></a>
</div>
</article>

How would you exclude all subcategories?

Thank you

Share Improve this question asked Jun 13, 2019 at 13:41 juanjuan 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1
$args = array(
  'orderby' => 'name',
  'order' => 'ASC',
  'parent' => 0
);
$categories = get_categories($args);

May this will help you. Refer this link

发布评论

评论列表(0)

  1. 暂无评论