0

我将实现基于标签的画廊过滤。但是,没有办法在 WordPress 中标记图像。

是否可以使用自定义分类法扩展 WordPress Gallery 来为每个附加的图片指定标签?

4

1 回答 1

1

有很多插件可以做到这一点,但如果你想要完整的解释: https ://wordpress.stackexchange.com/questions/76720/how-it-is-possible-to-use-taxonomies-on-attachments

但是,还有另一种方法可以实现您想要的(以及更多) - 即使它不是“本机”,也就是在媒体上使用自定义字段。

现在不要误会我的意思,我不是自定义字段的忠实粉丝,我一直认为人们“滥用”它们,但在这种情况下,他们过去曾为我工作过......

/******************************
* ADD CUSTOM FIELDS TO MEDIA ITEMS
* If you really want you can make it a stand-alone plugin. 
* I guess you know how ..
******************************/

    class o99_CustomMediaFields {
        // your array of fields 
        private $aOptions = array('normal' => 'Normal Image', 'polaroid' => 'Polaroid Image', 'bw' => 'Black & White Image', 'beauty' => 'Beauty Image', 'photoset' => 'Photoset');
        function __construct() {
            add_filter('attachment_fields_to_edit', array(&$this, 'handleAttachmentFields'), 10, 2);
            add_action('edit_attachment', array(&$this, 'handleAttachmentSave'));
            add_filter('manage_media_columns', array(&$this, 'handleColumnAdd'));
            add_action('manage_media_custom_column', array(&$this, 'handleColumnContent'),10,2);
        }
        public function handleAttachmentFields($form_fields, $post) {
            foreach($this->aOptions as $sKey => $sName) {
                $bOpt = (bool)get_post_meta($post->ID, 'image_'.$sKey, true);
                $form_fields['image_'.$sKey] = array(
                    'label' => __('Is ').$sName,
                    'input' => 'html',
                    'html' => '<label for="attachments-'.$post->ID.'-'.$sKey.'"> <input type="checkbox" id="attachments-'.$post->ID.'-'.$sKey.'" name="attachments['.$post->ID.']['.$sKey.']" value="1"'.($bOpt ? ' checked="checked"' : '').' /> </label>',
                    'value' => $bOpt,
                );
            }
            return $form_fields;
        }
        public function handleAttachmentSave($attachment_id) {
            foreach($this->aOptions as $sKey => $sName) {
                if (isset($_REQUEST['attachments'][$attachment_id][$sKey])) {
                    add_post_meta($attachment_id, 'image_'.$sKey, '1', true) or update_post_meta($attachment_id, 'image_'.$sKey, '1');
                } else {
                    add_post_meta($attachment_id, 'image_'.$sKey, '0', true) or update_post_meta($attachment_id, 'image_'.$sKey, '0');
                }
            }
        }
        public function handleColumnAdd($columns) {
            $columns['image_type'] = 'Image Type';
            return $columns;
        }
        public function handleColumnContent($column_name, $id) {
            if ($column_name != 'image_type') { return; }
            $sType = '';
            foreach($this->aOptions as $sKey => $sName) {
                $bOpt = (bool)get_post_meta($id, 'image_'.$sKey, true);
                if ($bOpt) { $sType .= $sName.', '; }
            }
            if (strlen($sType) >= 2) { $sType = substr($sType,0,-2); }
            echo $sType;
        }
    }
    new o99_CustomMediaFields();
于 2013-09-09T14:36:14.200 回答