category image taxonomy

时间:2015-10-08 作者:Noemi cogo

我正在使用此解决方案解决我的问题,为图像创建分类法,而不使用任何插件,如下所示:How to add upload field in a custom taxonomy?

这是我使用的代码:

    function edit_form_tag( ) {
    echo \' enctype="multipart/form-data"\';
}
add_action( \'category_term_edit_form_tag\' , \'edit_form_tag\' );
add_action( \'tax_projects_term_edit_form_tag\' , \'edit_form_tag\' );

add_action( \'tax_projects_term_edit_form_tag\' , \'edit_form_tag\' );

/** Add New Field To Category **/
function additional_category_fields( $term, $tax ) {
    $uploadID   = get_option( "{$tax}_image_{$term->term_id}" );            // Retrieve our Attachment ID from the Options Database Table
    $feedback   = get_option( "{$tax}_image_{$term->term_id}_feedback" );   // Retrieve any upload feedback from the Optoins Database Table
?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="meta-order"><?php _e( \'Category Image\' ); ?></label></th>
        <td>
            <div id="catImage">

                <!-- Create a nonce to validate against -->
                <input type="hidden" name="upload_meta_nonce" value="<?php echo wp_create_nonce( basename( __FILE__ ) ); ?>" />

                <!-- Define our actual upload field -->
                Please choose an image: <input type="file" name="_uploaded_file" value="" />

                <?php 
                  if( is_numeric( $uploadID ) ) :                                       // IF our upload ID is actually numeric, proceed

                    /***
                    /*  In this case we are pulling an image, if we are uploading
                    /*  something such as a PDF we could use the built-in function
                    /*  wp_get_attachment_url( $id );
                    /*  codex.wordpress.org/Function_Reference/wp_get_attachment_url
                    ***/
                    $imageArr = wp_get_attachment_image_src( $uploadID, \'medium\' );     // Get the URL of the medium sized image
                    $imageURL = $imageArr[0];                                           // wp_get_attachment_image_src() returns an array, index 0 is our URL
                ?>

                    <div id="uploaded_image">
                        <a href="post.php?post=<?php echo $uploadID; ?>&action=edit" target="_blank">Edit Image</a><br />

                        <!-- Display our image using the URL retrieved earlier -->
                        <a href="post.php?post=<?php echo $uploadID; ?>&action=edit" target="_blank"><img src="<?php echo $imageURL; ?>" /></a><br /><br />
                    </div>

                <!-- IF we received feedback, something went wrong and we need to show that feedback. -->               
                <?php elseif( ! empty( $feedback ) ) : ?>

                    <p style="color:red;font-size:12px;font-weight;bold;font-style:italic;"><?php echo $feedback; ?></p>

                <?php endif; ?>

            </div>
            <span class="description"><?php _e( \'Upload an appropriate image.\' ); ?></span>
                <br />
                <br />

            <!-- This link is for our deletion process -->
            <?php if( ! empty( $uploadID ) ) : ?>

                <a href="javascript:void(0)" class="deleteImage" style="color:red;text-decoration:underline;">Delete</a>

            <?php endif; ?>

        </td> 
    </tr>
<?php
    /** Since we\'ve shown the user the feedback they need to see, we can delete our option **/
    delete_option( "{$tax}_image_{$term->term_id}_feedback" );
}
add_action( \'category_edit_form_fields\', \'additional_category_fields\', 10, 2 ); 

/** Save Category Meta **/
function save_category_fields( $term_id ) {

    // Make sure that the nonce is set, taxonomy is set, and that our uploaded file is not empty
    if(
      isset( $_POST[\'upload_meta_nonce\'] ) && wp_verify_nonce( $_POST[\'upload_meta_nonce\'], basename( __FILE__ ) ) &&
      isset( $_POST[\'taxonomy\'] ) && isset( $_FILES[\'_uploaded_file\'] ) && !empty( $_FILES[\'_uploaded_file\'] )
    ) {
        $tax            = $_POST[\'taxonomy\'];                                                   // Store our taxonomy, used for the option naming convention
        $supportedTypes = array( \'image/gif\', \'image/jpeg\', \'image/png\' );                      // Only accept image mime types. - List of mimetypes: http://en.wikipedia.org/wiki/Internet_media_type
        $fileArray      = wp_check_filetype( basename( $_FILES[\'_uploaded_file\'][\'name\'] ) );   // Get the mime type and extension.
        $fileType       = $fileArray[\'type\'];                                                   // Store our file type

        // Verify that the type given is what we\'re expecting
        if( in_array( $fileType, $supportedTypes ) ) {
            $uploadStatus = wp_handle_upload( $_FILES[\'_uploaded_file\'], array( \'test_form\' => false ) );   // Let WordPress handle the upload

            // Make sure that the file was uploaded correctly, without error
            if( isset( $uploadStatus[\'file\'] ) ) {
                require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');

                // Let\'s add the image to our media library so we get access to metadata
                $imageID = wp_insert_attachment( array(
                        \'post_mime_type\'    => $uploadStatus[\'type\'],
                        \'post_title\'        => preg_replace( \'/\\.[^.]+$/\', \'\', basename( $uploadStatus[\'file\'] ) ),
                        \'post_content\'      => \'\',
                        \'post_status\'       => \'publish\'
                    ),
                    $uploadStatus[\'file\']
                );

                // Generate our attachment metadata then update the file.
                $attachmentData = wp_generate_attachment_metadata( $imageID, $uploadStatus[\'file\'] );
                wp_update_attachment_metadata( $imageID,  $attachmentData );


                $existingImage = get_option( "{$tax}_image_{$term_id}" );               // IF a file already exists in this option, grab it
                if( ! empty( $existingImage ) && is_numeric( $existingImage ) ) {       // IF the option does exist, delete it.
                    wp_delete_attachment( $existingImage );
                }

                update_option( "{$tax}_image_{$term_id}", $imageID );                   // Update our option with the new attachment ID
                delete_option( "{$tax}_image_{$term_id}_feedback" );                    // Just in case there\'s a feedback option, delete it - theoretically it shouldn\'t exist at this point.
            }
            else {
                $uploadFeedback = \'There was a problem with your uploaded file. Contact Administrator.\';    // Something major went wrong, enable debugging
            }
        }
        else {
            $uploadFeedback = \'Image Files only: JPEG/JPG, GIF, PNG\';   // Wrong file type
        }

        // Update our Feedback Option
        if( isset( $uploadFeedback ) ) {
            update_option( "{$tax}_image_{$term_id}_feedback", $uploadFeedback );
        }
    }
}
add_action ( \'edited_category\', \'save_category_fields\');
我的问题很简单,我是php新手,我想知道如何输出这个值。我试过这样做:

<?php
$image_of = get_option(\'category_image_1\');
echo $image_of;
?>
我在步骤3中使用了“get\\u option(\'category\\u image\\u 1\')”,正如上面的链接所说的那样,但我不知道在echo之前我必须输入什么。。有什么建议吗?

谢谢诺埃米

1 个回复
SO网友:Noemi cogo

Found a solution:

<?php 
    $cat = get_the_category();
    $category_id = $cat[0]->term_id;


                foreach (get_the_category() as $cat) :




                $attachmentId = get_option("category_image_{$category_id}", \'null\');
                $imageArr = wp_get_attachment_image_src( $attachmentId, \'medium\' );     
                $imageURL = $imageArr[0];                                           
                echo \'<img src="\' .  $imageURL . \'" />\';

                endforeach; ?>

相关推荐

Convert emoji to images

我有一个私有插件,可以导出所有没有“主题化”的帖子,所以输出的只是内容的html(帖子主题为H1,日期/时间为H2)。如果帖子包含表情符号,它会正确显示在主题页面上。但是如果我将HTML输出保存到一个文件中,表情符号会显示为&eth;&#159;&#153;&#129; 这些是UniCode字符(我想),我需要生成的HTML页面将表情符号显示为图形图像。创建HTML输出的代码使用标准WP\\u查询对象,然后在have\\u posts循环中输出内容:$thest