Custom Attachment Type

时间:2013-01-12 作者:Ünsal Korkmaz

我想创建一个“摄影”自定义帖子类型,它使用附件的相同编辑/上传面板。“附件”帖子类型的面板用法完全相同,但带有“摄影”名称。有可能吗?

3 个回复
最合适的回答,由SO网友:Ünsal Korkmaz 整理而成

目前看来,使用WordPress 3.5是不可能的。我提出了一个想法:http://wordpress.org/extend/ideas/topic/custom-attachment-type若你们喜欢这个想法,请支持。

SO网友:Genxer

Adding Additional Fields

function add_image_attachment_fields_to_edit( $form_fields, $post ) {

    // Remove the "Description" field, we\'re not using it
    unset( $form_fields[\'post_content\'] ); 

    // Add description text (helps) to the "Title" field
    $form_fields[\'post_title\'][\'helps\'] = \'Use a descriptive title for the image. This will make it easy to find the image in the future and will improve SEO.\';

    // Re-order the "Caption" field by removing it and re-adding it later
    $form_fields[\'post_excerpt\'][\'helps\'] = \'Describe the significants of the image pertaining to the site.\';
    $caption_field = $form_fields[\'post_excerpt\'];
    unset($form_fields[\'post_excerpt\']);

    // Re-order the "File URL" field
    $image_url_field = $form_fields[\'image_url\'];
    unset($form_fields[\'image_url\']);

    // Add Caption before Credit field 
    $form_fields[\'post_excerpt\'] = $caption_field;

    // Add a Credit field
    $form_fields["credit_text"] = array(
        "label" => __("Credit"),
        "input" => "text", // this is default if "input" is omitted
        "value" => esc_attr( get_post_meta($post->ID, "_credit_text", true) ),
        "helps" => __("The owner of the image."),
    );

    // Add a Credit field
    $form_fields["credit_link"] = array(
        "label" => __("Credit URL"),
        "input" => "text", // this is default if "input" is omitted
        "value" => esc_url( get_post_meta($post->ID, "_credit_link", true) ),
        "helps" => __("Attribution link to the image source or owners website."),
    );

    // Add Caption before Credit field 
    $form_fields[\'image_url\'] = $image_url_field;

    return $form_fields;
}
add_filter("attachment_fields_to_edit", "add_image_attachment_fields_to_edit", null, 2);

Save The Data as Custom Fields

function add_image_attachment_fields_to_save( $post, $attachment ) {
    if ( isset( $attachment[\'credit_text\'] ) )
        update_post_meta( $post[\'ID\'], \'_credit_text\', esc_attr($attachment[\'credit_text\']) );

    if ( isset( $attachment[\'credit_link\'] ) )
        update_post_meta( $post[\'ID\'], \'_credit_link\', esc_url($attachment[\'credit_link\']) );

    return $post;
}
add_filter("attachment_fields_to_save", "add_image_attachment_fields_to_save", null , 2);

Display Attachment Custom Fields

function base_image_credit( $post_ID = null ) {
    // Get the post ID of the current post if not set
    if ( !$post_ID ) {
        global $post;
        $post_ID = $post->ID;
    }

    // Get all the attachments for the current post (object stdClass)
    $attachments = get_children(\'post_type=attachment&post_parent=\' . $post->ID);

    // If attachments are found
    if ( isset($attachments) && !empty($attachments) ) {
        // Get the first attachment
        $first_attachment = current($attachments);
        $attachment_fields = get_post_custom( $first_attachment->ID );

        // Get custom attachment fields
        $credit_text = ( isset($attachment_fields[\'_credit_text\'][0]) && !empty($attachment_fields[\'_credit_text\'][0]) ) ? esc_attr($attachment_fields[\'_credit_text\'][0]) : \'\';
        $credit_link = ( isset($attachment_fields[\'_credit_link\'][0]) && !empty($attachment_fields[\'_credit_link\'][0]) ) ? esc_url($attachment_fields[\'_credit_link\'][0]) : \'\';

        // Output HTML if you want
        $credit = ( $credit_text && $credit_link ) ? "Image provided by <a href=\'$credit_link\'>$credit_text</a>" : false;
    }

    return $credit;
}

Using the display image credit function

此函数可以在插件或主题中的任何位置使用。我在single上使用了该功能。像这样的php

<?php if( function_exists(\'base_image_credit\') ) echo base_image_credit(); ?>
原始的tutorial

当做

SO网友:Genxer

您仍然可以在主题函数中添加代码。php文件使其工作。

function be_attachment_field_credit( $form_fields, $post ) {
        $form_fields[\'be-photographer-name\'] = array(
            \'label\' => \'Photographer Name\',
            \'input\' => \'text\',
            \'value\' => get_post_meta( $post->ID, \'be_photographer_name\', true ),
            \'helps\' => \'If provided, photo credit will be displayed\',
        );

        $form_fields[\'be-photographer-url\'] = array(
            \'label\' => \'Photographer URL\',
            \'input\' => \'text\',
            \'value\' => get_post_meta( $post->ID, \'be_photographer_url\', true ),
            \'helps\' => \'Add Photographer URL\',
        );

        return $form_fields;
    }

    add_filter( \'attachment_fields_to_edit\', \'be_attachment_field_credit\', 10, 2 );

    /**
     * Save values of Photographer Name and URL in media uploader
     *
     * @param $post array, the post data for database
     * @param $attachment array, attachment fields from $_POST form
     * @return $post array, modified post data
     */

    function be_attachment_field_credit_save( $post, $attachment ) {
        if( isset( $attachment[\'be-photographer-name\'] ) )
            update_post_meta( $post[\'ID\'], \'be_photographer_name\', $attachment[\'be-photographer-name\'] );

        if( isset( $attachment[\'be-photographer-url\'] ) )
    update_post_meta( $post[\'ID\'], \'be_photographer_url\', esc_url( $attachment[\'be-photographer-url\'] ) );

        return $post;
    }

    add_filter( \'attachment_fields_to_save\', \'be_attachment_field_credit_save\', 10, 2 );
    ?>
如果要显示附件模板中的字段,只需将以下代码粘贴到循环中即可:

echo get_post_meta($post->ID, \'be_photographer_url\', true);
如果要在存档模板或任何其他模板中显示特色图像的字段,只需使用:

echo get_post_meta(get_post_thumbnail_id(), \'be_photographer_url\', true);

结束