0

I have 20 subcategories from a parent category, and they are like that
Parent

  • September
  • November
  • … up to 20

Here is the code that try it to display, but I get only 1 category instead

<?php
$categories = get_categories('child_of=505');
foreach($categories as $category): ?>

    <div class="qitem">
        <a href="<?php get_category_link( $category->term_id ); ?>" title="<?php echo $category->name; ?>">
            <?php echo $category->name; ?>
        </a>
        <h4>
            <span class="caption">
                <?php echo $category->name; ?>
            </span>
        </h4>
        <p>
            <span class="caption">
                <?php echo $category->description; ?>
            </span>
        </p>
    </div>

<?php endforeach; ?>
2
  • 1
    Is this the complete code? You're missing an endforeach. Oct 5 at 5:57
  • I missed to put the endforeach in the post. Oct 5 at 5:58
0

get_categories() uses get_terms() so the array argument you can pass it will be the same.
From the documentation the array can have a property hide_empty, by default its true.
I'm guessing that those categories have no posts attached to it, if that is the case you will need to set it to false.
The code would be like this.

get_categories([
    'child_of'   => 505,
    'hide_empty' => false
]);

Update

If you want to order the results you can add order to the array, the default value is ASC so you will need to set it to DESC.

get_categories([
    'child_of'   => 505,
    'hide_empty' => false,
    'order'      => 'DESC'
]);

You can check WP_Term_Query::__construct( string|array $query = '' ) for all the available arguments you can use.

5
  • Its works,Thanks. Any chance to display on DESC order? Oct 5 at 6:41
  • Find it 'order'=> 'DESC' Oct 5 at 6:44
  • One more question the href is not working. It send to /blog Oct 5 at 6:46
  • @DigitalPoint the problem is <?php get_category_link( $category->term_id ); ?>, this returns the value but you never outputed it. you can do <?php echo get_category_link( $category->term_id ); ?> , or my personal preference <?= get_category_link( $category->term_id ); ?> Oct 5 at 6:48
  • Oh, ok lol. Thanks so much Oct 5 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.