How to list categories in reverse alphabetical order?

I am organising posts on a blog-type website into "issues", each containing 10 articles and assigned a unique

I am organising posts on a blog-type website into "issues", each containing 10 articles and assigned a unique category such as issue7.

Using a sidebar widget I want to list these categories so that a user can click on "Issue 7" and they will bring up all the articles in category issue7, namely articles 61-70.

It's straightforward to list them in alphabetical order of the category names. But how can I list them in reverse alphabetical order of those names, such that the link "Issue 3" (for category issue3) appears before "Issue 2" and the link to the latest "issue" appears at the top?

I'm using Astra with a child theme. I've tried putting the following into functions.php in the child theme, but to no effect:

$categories = get_terms( 'category', array(
'orderby'    => 'name',
'order'      => 'DESC',
'hide_empty' => 0
) );

(I realise that once I go over Issue 9 I will probably have to rename category issue1 as issue01.)

I am organising posts on a blog-type website into "issues", each containing 10 articles and assigned a unique category such as issue7.

Using a sidebar widget I want to list these categories so that a user can click on "Issue 7" and they will bring up all the articles in category issue7, namely articles 61-70.

It's straightforward to list them in alphabetical order of the category names. But how can I list them in reverse alphabetical order of those names, such that the link "Issue 3" (for category issue3) appears before "Issue 2" and the link to the latest "issue" appears at the top?

I'm using Astra with a child theme. I've tried putting the following into functions.php in the child theme, but to no effect:

$categories = get_terms( 'category', array(
'orderby'    => 'name',
'order'      => 'DESC',
'hide_empty' => 0
) );

(I realise that once I go over Issue 9 I will probably have to rename category issue1 as issue01.)

Share Improve this question edited Jun 7, 2019 at 11:57 asked Jun 7, 2019 at 11:50 user169591user169591
Add a comment  | 

1 Answer 1

Reset to default 1

Redefine arguments for 'category' taxonomy ONLY:

add_filter( 'get_terms_args', 'my_term_args', 10, 2 );

function my_term_args( $args, $taxonomies ) {
    // don't affect admin area passing back default arguments
    if ( is_admin() ) {
        return $args;
    }

    // check the taxonomy in use and redefine it's default arguments
    if( in_array( 'category', $taxonomies ) ) {
        $args['orderby']    = 'name';
        $args['order']      = 'DESC';
        $args['hide_empty'] = false;
    }

    return $args;
}

Also, get_terms() syntax you've posted is obsolete since version 4.5. The new syntax look like this:

<?php
$categories = get_terms( array(
    'taxonomy'   => 'category'
    'orderby'    => 'name',
    'order'      => 'DESC',
    'hide_empty' => false
) );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745428216a4627274.html

相关推荐

  • How to list categories in reverse alphabetical order?

    I am organising posts on a blog-type website into "issues", each containing 10 articles and assigned a unique

    3小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信