如何在新媒体库的附件上使用分类法?


9

WordPress 3.5更改了媒体管理,现在“媒体库”编辑屏幕使用默认的帖子类型UI。对于具有不同用户和附件的WP安装,分类法非常有用,它为查找附件和/或添加分类提供了更多可能性。

我已经看到可以在附件中添加类别Metabox吗?在WPSE上使用,但不是完美地与WP 3.5一起使用,并且也没有关于在附件上使用自定义类别的信息,不仅是帖子的类别。

简而言之:是否可以将自定义类别/标签添加到附件,以在WP 3.5的媒体库中使用?

Answers:


15

要从默认的post类型post中添加分类法,则可以使用一个小的插件轻松添加分类法“ category”和“ tags”,只需点击下面的源即可。

<?php
/**
 * Plugin Name: Attachment Taxonomies
 * Plugin URI:  
 * Text Domain: attachment_taxonomies
 * Domain Path: /languages
 * Description: 
 * Version:     1.0.0
 * Author:      Frank Bültge
 * Author URI:  http://bueltge.de
 * License:     GPLv3
 */


add_action( 'init', 'fb_attachment_taxonomies' );
function fb_attachment_taxonomies() {

    $taxonomies = array( 'category', 'post_tag' ); // add the 2 tax to ...
    foreach ( $taxonomies as $tax ) {
        register_taxonomy_for_object_type( $tax, 'attachment' ); // add to post type attachment
    }
}

为了在附件上使用自定义分类法,重要的是创建一个自定义分类法,并将其设置为post类型attachment,例如follow插件。

<?php
/**
 * Plugin Name: Attachment Taxonomies
 * Plugin URI:  
 * Text Domain: attachment_taxonomies
 * Domain Path: /languages
 * Description: 
 * Version:     1.0.0
 * Author:      Frank Bültge
 * Author URI:  http://bueltge.de
 * License:     GPLv3
 */

if ( function_exists( 'add_filter' ) )
    add_action( 'plugins_loaded', array( 'Fb_Attachment_Taxonomies', 'get_object' ) );
/**
 * Add Tags and Categories taxonmies to Attachment with WP 3.5
 */
class Fb_Attachment_Taxonomies {

    static private $classobj;

    /**
     * Constructor, init the functions inside WP
     *
     * @since   1.0.0
     * @return  void
     */
    public function __construct() {

        // load translation files
        add_action( 'admin_init', array( $this, 'localize_plugin' ) );
        // add taxonmies
        add_action( 'init', array( $this, 'setup_taxonomies' ) );
    }

    /**
     * Handler for the action 'init'. Instantiates this class.
     *
     * @since   1.0.0
     * @access  public
     * @return  $classobj
     */
    public function get_object() {

        if ( NULL === self::$classobj ) {
            self::$classobj = new self;
        }

        return self::$classobj;
    }

    /**
     * Localize plugin function.
     *
     * @uses    load_plugin_textdomain, plugin_basename
     * @since   2.0.0
     * @return  void
     */
    public function localize_plugin() {

        load_plugin_textdomain(
            'attachment_taxonomies',
            FALSE,
            dirname( plugin_basename( __FILE__ ) ) . '/languages/'
        );
    }

    /**
     * Setup Taxonomies
     * Creates 'attachment_tag' and 'attachment_category' taxonomies.
     * Enhance via filter `fb_attachment_taxonomies`
     * 
     * @uses    register_taxonomy, apply_filters
     * @since   1.0.0
     * @return  void
     */
    public function setup_taxonomies() {

        $attachment_taxonomies = array();

        // Tags
        $labels = array(
            'name'              => _x( 'Media Tags', 'taxonomy general name', 'attachment_taxonomies' ),
            'singular_name'     => _x( 'Media Tag', 'taxonomy singular name', 'attachment_taxonomies' ),
            'search_items'      => __( 'Search Media Tags', 'attachment_taxonomies' ),
            'all_items'         => __( 'All Media Tags', 'attachment_taxonomies' ),
            'parent_item'       => __( 'Parent Media Tag', 'attachment_taxonomies' ),
            'parent_item_colon' => __( 'Parent Media Tag:', 'attachment_taxonomies' ),
            'edit_item'         => __( 'Edit Media Tag', 'attachment_taxonomies' ), 
            'update_item'       => __( 'Update Media Tag', 'attachment_taxonomies' ),
            'add_new_item'      => __( 'Add New Media Tag', 'attachment_taxonomies' ),
            'new_item_name'     => __( 'New Media Tag Name', 'attachment_taxonomies' ),
            'menu_name'         => __( 'Media Tags', 'attachment_taxonomies' ),
        );

        $args = array(
            'hierarchical' => FALSE,
            'labels'       => $labels,
            'show_ui'      => TRUE,
            'show_admin_column' => TRUE,
            'query_var'    => TRUE,
            'rewrite'      => TRUE,
        );

        $attachment_taxonomies[] = array(
            'taxonomy'  => 'attachment_tag',
            'post_type' => 'attachment',
            'args'      => $args
        );

        // Categories
        $labels = array(
            'name'              => _x( 'Media Categories', 'taxonomy general name', 'attachment_taxonomies' ),
            'singular_name'     => _x( 'Media Category', 'taxonomy singular name', 'attachment_taxonomies' ),
            'search_items'      => __( 'Search Media Categories', 'attachment_taxonomies' ),
            'all_items'         => __( 'All Media Categories', 'attachment_taxonomies' ),
            'parent_item'       => __( 'Parent Media Category', 'attachment_taxonomies' ),
            'parent_item_colon' => __( 'Parent Media Category:', 'attachment_taxonomies' ),
            'edit_item'         => __( 'Edit Media Category', 'attachment_taxonomies' ), 
            'update_item'       => __( 'Update Media Category', 'attachment_taxonomies' ),
            'add_new_item'      => __( 'Add New Media Category', 'attachment_taxonomies' ),
            'new_item_name'     => __( 'New Media Category Name', 'attachment_taxonomies' ),
            'menu_name'         => __( 'Media Categories', 'attachment_taxonomies' ),
        );

        $args = array(
            'hierarchical' => TRUE,
            'labels'       => $labels,
            'show_ui'      => TRUE,
            'query_var'    => TRUE,
            'rewrite'      => TRUE,
        );

        $attachment_taxonomies[] = array(
            'taxonomy'  => 'attachment_category',
            'post_type' => 'attachment',
            'args'      => $args
        );

        $attachment_taxonomies = apply_filters( 'fb_attachment_taxonomies', $attachment_taxonomies );

        foreach ( $attachment_taxonomies as $attachment_taxonomy ) {
            register_taxonomy(
                $attachment_taxonomy['taxonomy'],
                $attachment_taxonomy['post_type'],
                $attachment_taxonomy['args']
            );
        }

    }

} // end class

在以下屏幕截图中查看结果,也可以看到差异-就像我对源代码所说的那样简单。但是示例屏幕截图中我的人的形象与来源无关;) 带有WP 3.5的默认帖子类型ui中的编辑媒体的屏幕截图

小提示:模式框中用于在帖子类型上添加媒体的用户界面与帖子类型附件上的编辑屏幕几乎没有什么不同。层次分类法在编辑屏幕中只有一棵树。在模式框中,它是一个输入字段,税以逗号作为分隔符。另请参阅这篇文章从海伦的WP核心博客。但请同时在屏幕截图中查看“标签”和“类别”的自定义分类法。

在模式框中编辑附件


1
+1+发现媒体库3.5又迈出了一步,这是2012年的大未知!
brasofilo 2012年

2
一个很好的补充就是论点'show_admin_column' => true
brasofilo 2012年

是的,你有权利。我喜欢WP 3.6中的这个参数;如果我使用mayn分类法,我经常将其与一个小助手类一起使用:github.com/bueltge/WP-Control-Taxonomy
bueltge 2012年

2
弗兰克,不要忘记,对于附件分类法,您可能应该设置update_count_callback_update_generic_term_count。见更新食品条目原因:codex.wordpress.org/Function_Reference/...
汤姆·奥格

2

我将通过向分类列表中的自定义帖子类型的管理员列表添加分类过滤器来扩展Frank的答案

在寻找媒体类别和分类过滤器这两者的同时,我在该帖子中将Frank的代码与Kaiser的答案合并了。还增加了我的一面,以将帖子上传类型(附件上传到的帖子类型)添加为类别。

它产生此:

过滤媒体类别

add_action(
    'plugins_loaded',
    array ( WPSE76720_Attachment_Taxonomies::get_object(), 'plugin_setup' )
);

// BUELTGE/KAISER/RUDOLF
class WPSE76720_Attachment_Taxonomies 
{
    protected static $instance = NULL;
    public $post_type;
    public $taxonomies;

    /**
     * Used for regular plugin work.
     *
     * @wp-hook plugins_loaded
     * @return  void
     */
    public function plugin_setup()
    {
        // Taxonomies filter
        add_action( 'load-upload.php', array( $this, 'setup' ) );
        // add taxonmies
        add_action( 'init', array( $this, 'setup_taxonomies' ) );
        add_action( 'add_attachment', array( $this, 'auto_tax' ), 10, 2 );
    }

    /**
     * Constructor, init the functions inside WP
     *
     * @since   1.0.0
     * @return  void
     */
    public function __construct() {}

    /**
     * Handler for the action 'init'. Instantiates this class.
     *
     * @since   1.0.0
     * @access  public
     * @return  $instance
     */
    public function get_object() 
    {
        NULL === self::$instance and self::$instance = new self;
        return self::$instance;
    }

    /**
     * Setup Taxonomies
     * Creates 'attachment_tag' and 'attachment_category' taxonomies.
     * Enhance via filter `fb_attachment_taxonomies`
     * 
     * @uses    register_taxonomy, apply_filters
     * @since   1.0.0
     * @return  void
     */
    public function setup_taxonomies() 
    {
        $attachment_taxonomies = array();
        // Categories
        $labels = array(
            'name'              => __( 'Media Categories', 'b5f-mc' ),
            'singular_name'     => __( 'Media Category', 'b5f-mc' ),
            'search_items'      => __( 'Search Media Categories', 'b5f-mc' ),
            'all_items'         => __( 'All Media Categories', 'b5f-mc' ),
            'parent_item'       => __( 'Parent Media Category', 'b5f-mc' ),
            'parent_item_colon' => __( 'Parent Media Category:', 'b5f-mc' ),
            'edit_item'         => __( 'Edit Media Category', 'b5f-mc' ), 
            'update_item'       => __( 'Update Media Category', 'b5f-mc' ),
            'add_new_item'      => __( 'Add New Media Category', 'b5f-mc' ),
            'new_item_name'     => __( 'New Media Category Name', 'b5f-mc' ),
            'menu_name'         => __( 'Media Categories', 'b5f-mc' ),
        );
        $args = array(
            'hierarchical' => TRUE,
            'labels'       => $labels,
            'show_admin_column' => TRUE,
            'show_ui'      => TRUE,
            'query_var'    => TRUE,
            'rewrite'      => TRUE,
        );
        $attachment_taxonomies[] = array(
            'taxonomy'  => 'attachment_category',
            'post_type' => 'attachment',
            'args'      => $args
        );
        $attachment_taxonomies = apply_filters( 'fb_attachment_taxonomies', $attachment_taxonomies );
        foreach ( $attachment_taxonomies as $attachment_taxonomy ) {
            register_taxonomy(
                $attachment_taxonomy['taxonomy'],
                $attachment_taxonomy['post_type'],
                $attachment_taxonomy['args']
            );
        }
    }

    public function setup()
    {
        add_action( current_filter(), array( $this, 'setup_vars' ), 20 );
        add_action( 'restrict_manage_posts', array( $this, 'get_select' ) );
        add_filter( "manage_taxonomies_for_attachment_columns", array( $this, 'add_columns' ) );
    }

    public function setup_vars()
    {
        $this->post_type = 'attachment';
        $this->taxonomies = get_object_taxonomies( $this->post_type );
    }

    public function add_columns( $taxonomies )
    {
        return array_merge(
             $taxonomies
            ,$this->taxonomies
        );
    }

    public function get_select()
    {
        $walker = new WCMF_walker;
        foreach ( $this->taxonomies as $tax )
        {
            wp_dropdown_categories( array(
                 'taxonomy'        => $tax
                ,'hide_if_empty'   => false
                ,'show_option_all' => sprintf(
                     get_taxonomy( $tax )->labels->all_items
                 )
                ,'hide_empty'      => false
                ,'hierarchical'    => is_taxonomy_hierarchical( $tax )
                ,'show_count'      => false
                ,'orderby'         => 'name'
                ,'selected'        => '0' !== get_query_var( $tax )
                    ? get_query_var( $tax )
                    : false
                ,'name'            => $tax
                ,'id'              => $tax
                ,'walker'          => $walker
            ) );
        }
    }

    /**
     * Add the parent post type as an attachment category
     * 
     * @author Rodolfo Buaiz
     */
    public function auto_tax( $post_id ) 
    {
        $the_p = get_post( $post_id );
        if( $the_p->post_parent > 0 ) 
        {
            $cpt = get_post_type( $the_p->post_parent );
            $term = term_exists( $cpt, 'attachment_category' );
            if( !$term )
                $term = wp_insert_term( $cpt, 'attachment_category' );

            wp_set_post_terms( $post_id, $term['term_id'], 'attachment_category', true );
        }
    }
} // end BUELTGE/KAISER/RUDOLF

// KAISER
class WCMF_walker extends Walker_CategoryDropdown
{
    var $tree_type = 'category';
    var $db_fields = array(
         'parent' => 'parent'
        ,'id'     => 'term_id'
    );
    public $tax_name;

    /**
     * @see   Walker::start_el()
     * @param  string $output Passed by reference. Used to append additional content.
     * @param  object $term   Taxonomy term data object.
     * @param  int    $depth  Depth of category. Used for padding.
     * @param  array  $args   Uses 'selected' and 'show_count' keys, if they exist.
     * @param  int    $id
     * @return void
     */
    function start_el( &$output, $term, $depth, $args, $id = 0 )
    {
        $pad = str_repeat( '&nbsp;', $depth * 3 );
        $cat_name = apply_filters( 'list_cats', $term->name, $term );

        $output .= sprintf(
             '<option class="level-%s" value="%s" %s>%s%s</option>'
            ,$depth
            ,$term->slug
            ,selected(
                 $args['selected']
                ,$term->slug
                ,false
             )
            ,"{$pad}{$cat_name}"
            ,$args['show_count']
                ? "&nbsp;&nbsp;({$term->count})"
                : ''
        );
    }
}
// end KAISER

我认为现在是个好主意,您可以创建一个Github存储库,我们可以帮助您维护解决方案。使用它和创建自定义增强功能更加容易。
bueltge

-1

我的媒体类别插件将为您完成此操作-它甚至可以清理媒体模态上的界面,因此您仍然会获得复选框列表,默认情况下,您获得的只是文本字段。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.