首页 文章

带有标签支持的WordPress图库

提问于
浏览
0

我将基于标签实现库过滤 . 但是,无法在WordPress中标记图像 .

是否可以使用自定义分类法扩展WordPress库,为图库中附加的每个图像指定标签?

1 回答

  • 1

    有很多plugins这样做,但如果你想要整个解释: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();
    

相关问题