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

customization - How to show Term ID beside Name Category with wp_dropdown_categories()

programmeradmin2浏览0评论

I have example code:

                        <?php 
                                $defaults = array(
                                    'show_option_all'   => '',
                                    'show_option_none'  => '',
                                    'orderby'           => 'id',
                                    'order'             => 'ASC',
                                    'show_count'        => 1,
                                    'hide_empty'        => 0,
                                    'child_of'          => 0,
                                    'exclude'           => '',
                                    'echo'              => 1,
                                    'selected'          => 1,
                                    'hierarchical'      => 1,
                                    'name'              => 'cat',
                                    'id'                => 'mySelect',
                                    'class'             => 'postform',
                                    'depth'             => 0,
                                    'tab_index'         => 1,
                                    'taxonomy'          => 'category',
                                    'hide_if_empty'     => false,
                                    'option_none_value' => -1,
                                    'value_field'       => 'name',
                                    'required'          => false,
                                );
                                wp_dropdown_categories( $defaults )
                        ?>

Html will have like this:

<option class="level-1" value="Name category 1">Name category 1 (38)</option>

How to show term ID beside Name category like this:

<option class="level-1" value="Name category 1">Name category 1 (ID-161) (38)</option>

Thanks for your help

I have example code:

                        <?php 
                                $defaults = array(
                                    'show_option_all'   => '',
                                    'show_option_none'  => '',
                                    'orderby'           => 'id',
                                    'order'             => 'ASC',
                                    'show_count'        => 1,
                                    'hide_empty'        => 0,
                                    'child_of'          => 0,
                                    'exclude'           => '',
                                    'echo'              => 1,
                                    'selected'          => 1,
                                    'hierarchical'      => 1,
                                    'name'              => 'cat',
                                    'id'                => 'mySelect',
                                    'class'             => 'postform',
                                    'depth'             => 0,
                                    'tab_index'         => 1,
                                    'taxonomy'          => 'category',
                                    'hide_if_empty'     => false,
                                    'option_none_value' => -1,
                                    'value_field'       => 'name',
                                    'required'          => false,
                                );
                                wp_dropdown_categories( $defaults )
                        ?>

Html will have like this:

<option class="level-1" value="Name category 1">Name category 1 (38)</option>

How to show term ID beside Name category like this:

<option class="level-1" value="Name category 1">Name category 1 (ID-161) (38)</option>

Thanks for your help

Share Improve this question asked Apr 22, 2020 at 11:30 tientruong0810tientruong0810 1
Add a comment  | 

1 Answer 1

Reset to default 0

You can add a custom hook to the functions.php for that:

function my_add_cat_id($category_name, $category) {
    return $category_name ." (ID-".$category->term_id.")";
}
add_filter('list_cats', 'my_add_cat_id', 10,2);
发布评论

评论列表(0)

  1. 暂无评论