如何显示附件媒体中的自定义字段值?

时间:2015-03-04 作者:Pedro Salusso

我在函数中创建了以下代码。php将自定义字段添加到附件媒体库。数据也会保存,但我无法将数据显示到模板文件中。有什么建议吗??

谢谢

        /**
         * Add custom field to media
        */
        function mytheme_attachment_fields( $fields, $post ) {

            $meta = get_post_meta($post->ID, \'title_en\', true);
            $fields[\'title_en\'] = array(
                \'label\' => \'Título (EN)\',
                \'input\' => \'text\',
                \'value\' => $meta,
                \'show_in_edit\' => true,
            );

            $meta_two = get_post_meta($post->ID, \'description_en\', true);
            $fields[\'description_en\'] = array(
                \'label\' => \'Descripción (EN)\',
                \'input\' => \'textarea\',
                \'value\' => $meta_two,
                \'show_in_edit\' => true,
            );    

            $meta_three = get_post_meta($post->ID, \'title_es\', true);
            $fields[\'title_es\'] = array(
                \'label\' => \'Título (ES)\',
                \'input\' => \'text\',
                \'value\' => $meta_three,
                \'show_in_edit\' => true,
            );    

            $meta_four = get_post_meta($post->ID, \'description_es\', true);
            $fields[\'description_es\'] = array(
                \'label\' => \'Descripción (ES)\',
                \'input\' => \'textarea\',
                \'value\' => $meta_four,
                \'show_in_edit\' => true,
            );


            return $fields;         
        }
        add_filter( \'attachment_fields_to_edit\', \'mytheme_attachment_fields\', 10, 2 );

        /**
         * Update custom field on save
        */
        function mytheme_update_attachment_meta($attachment){
            global $post;
              update_post_meta($post->ID, \'title_en\', $attachment[\'attachments\'][$post->ID][\'title_en\']);
              update_post_meta($post->ID, \'description_en\', $attachment[\'attachments\'][$post->ID][\'description_en\']);
              update_post_meta($post->ID, \'title_es\', $attachment[\'attachments\'][$post->ID][\'title_es\']); 
              update_post_meta($post->ID, \'description_es\', $attachment[\'attachments\'][$post->ID][\'description_es\']);  
            return $attachment;
        }
        add_filter( \'edit_attachment\', \'mytheme_update_attachment_meta\', 4);

        /**
         * Update custom field via ajax
        */
        function mytheme_media_xtra_fields() {
            $post_id = $_POST[\'id\'];
            $meta = $_POST[\'attachments\'][$post_id ][\'title_en\'];
              update_post_meta($post_id , \'title_en\', $meta);
            $meta_two = $_POST[\'attachments\'][$post_id ][\'description_en\'];
              update_post_meta($post_id , \'description_en\', $meta_two); 
            $meta_three = $_POST[\'attachments\'][$post_id ][\'title_es\'];
              update_post_meta($post_id , \'title_es\', $meta_three);
            $meta_four = $_POST[\'attachments\'][$post_id ][\'description_es\'];
              update_post_meta($post_id , \'description_es\', $meta_four); 
            clean_post_cache($post_id);
        }
        add_action(\'wp_ajax_save-attachment-compat\', \'mytheme_media_xtra_fields\', 0, 1);

1 个回复
SO网友:Firsh - justifiedgrid.com

使用这些:

get_post_meta($attachment->ID, \'_title_en\', true);
get_post_meta($attachment->ID, \'_description_en\', true);
get_post_meta($attachment->ID, \'_title_es\', true);
get_post_meta($attachment->ID, \'_description_es\', true);
查看是否需要使用前缀:Creating Custom Fields for Attachments in Wordpress

此外:Function Reference/get post meta

结束

相关推荐

how to edit attachments?

在将例如文件附加到帖子时,如何在事后编辑/删除它们?在帖子编辑器中找不到任何内容。谢谢