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

Custom taxonomy filter for media

programmeradmin3浏览0评论

I have the following code already working in the admin for my media library:

function atelier_add_iCat_taxonomy() {
    $labels = array(
        'name'              => 'iCats',
        'singular_name'     => 'iCat',
        'search_items'      => 'Search iCats',
        'all_items'         => 'All iCats',
        'parent_item'       => 'Parent iCat',
        'parent_item_colon' => 'Parent iCat:',
        'edit_item'         => 'Edit iCat',
        'update_item'       => 'Update iCat',
        'add_new_item'      => 'Add New iCat',
        'new_item_name'     => 'New iCat Name',
        'menu_name'         => 'iCat',
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'query_var' => 'true',
        'rewrite' => 'true',
        'show_admin_column' => 'true',
    );

    register_taxonomy( 'iCat', 'attachment', $args );
}
add_action( 'init', 'atelier_add_iCat_taxonomy' );

I would like a filter to make sure that images belonging to a certain category are only displayed.

Have tried the following, but it displays the main categories and not the custom taxonomy I have.

function atelier_add_image_category_filter() {
    $screen = get_current_screen();
    if ( 'upload' == $screen->id ) {
        $dropdown_options = array( 'show_option_all' => __( 'View all categories', 'iCats' ), 'hide_empty' => false, 'hierarchical' => true, 'orderby' => 'name', );
        wp_dropdown_categories( $dropdown_options );
    }
}
add_action( 'restrict_manage_posts', 'atelier_add_image_category_filter' );

Am I missing something or is there another solution?

I have the following code already working in the admin for my media library:

function atelier_add_iCat_taxonomy() {
    $labels = array(
        'name'              => 'iCats',
        'singular_name'     => 'iCat',
        'search_items'      => 'Search iCats',
        'all_items'         => 'All iCats',
        'parent_item'       => 'Parent iCat',
        'parent_item_colon' => 'Parent iCat:',
        'edit_item'         => 'Edit iCat',
        'update_item'       => 'Update iCat',
        'add_new_item'      => 'Add New iCat',
        'new_item_name'     => 'New iCat Name',
        'menu_name'         => 'iCat',
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'query_var' => 'true',
        'rewrite' => 'true',
        'show_admin_column' => 'true',
    );

    register_taxonomy( 'iCat', 'attachment', $args );
}
add_action( 'init', 'atelier_add_iCat_taxonomy' );

I would like a filter to make sure that images belonging to a certain category are only displayed.

Have tried the following, but it displays the main categories and not the custom taxonomy I have.

function atelier_add_image_category_filter() {
    $screen = get_current_screen();
    if ( 'upload' == $screen->id ) {
        $dropdown_options = array( 'show_option_all' => __( 'View all categories', 'iCats' ), 'hide_empty' => false, 'hierarchical' => true, 'orderby' => 'name', );
        wp_dropdown_categories( $dropdown_options );
    }
}
add_action( 'restrict_manage_posts', 'atelier_add_image_category_filter' );

Am I missing something or is there another solution?

Share Improve this question edited Sep 26, 2016 at 15:55 Dave Romsey 17.9k11 gold badges56 silver badges70 bronze badges asked Sep 26, 2016 at 15:38 JCJJCJ 371 silver badge10 bronze badges 2
  • Just a little note, The taxonomy name should only contain lowercase letters. I don't think that's the solution here, but it might save you some headaches too. – Dave Romsey Commented Sep 26, 2016 at 16:03
  • 1 Thanks for the heads up @DaveRomsey - will amend site code – JCJ Commented Sep 26, 2016 at 16:15
Add a comment  | 

1 Answer 1

Reset to default 1

You can add the taxonomy parameter.

function add_image_category_filter() {
    $screen = get_current_screen();
    if ( 'upload' == $screen->id ) {
        $dropdown_options = array( 
            'taxonomy' => 'YOUR_TAXONOMY', 
            'show_option_all' => __( 'View all categories', 'iCats' ), 
            'hide_empty' => true, 
            'hierarchical' => true,
             // default is cat which wouldn't filter custom taxonomies
            'value_field'       => 'slug',
            'name'              => 'YOUR_TAXONOMY', 
            'orderby' => 'name', );
        wp_dropdown_categories( $dropdown_options );
    }
}
add_action( 'restrict_manage_posts', 'add_image_category_filter' );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论