Show thumbnail for category?

时间:2013-04-15 作者:user31532

我目前有一个代码,在我的类别中。php中,它检查特定类别是否有子类别,如果有,则使用wp\\u list\\u类别显示它们。

我很想让这些wp\\U list\\u类别显示缩略图,所以我创建了一个新的walker,但我不知道如何显示缩略图。

我很乐意让它显示我在这个类别中最新自定义帖子类型的特色图片,或者使用插件。

目前,我的功能中有以下助行器。php:

 class Walker_Category_Parents extends Walker_Category {



function start_el(&$output, $category, $depth, $args) {
    global $wpdb;
    extract($args);


    $link2 = \'\'.$category->slug.\'\';
    $cat_name = esc_attr( $category->name );
    $cat_name = apply_filters( \'list_cats\', $cat_name, $category );
    $link = \'<a href="\' . esc_attr( get_term_link($category) ) . \'" \';
    $link .= \'title="\' . esc_attr( strip_tags( apply_filters( \'category_description\', $category->description, $category ) ) ) . \'"\';
    $link .= \'rel="\'.$category->slug.\'" \';
    $link .= \'>\';
    $link .= $cat_name . \'</a>\';
    if ( \'list\' == $args[\'style\'] ) {
        $output .= "\\t<li";

        $children = $wpdb->get_results( "SELECT term_id FROM $wpdb->term_taxonomy WHERE parent=".$category->term_id );

        $children_count = count($children);

        $has_children = ($children_count != 0) ? \' parent-category\' : \'\';

        $class = \'cat-item cat-item-\' . $category->term_id . $has_children;
        if ( !empty($current_category) ) {
            $_current_category = get_term( $current_category, $category->taxonomy );
            if ( $category->term_id == $current_category )
                $class .=  \' current-cat\';
            elseif ( $category->term_id == $_current_category->parent )
                $class .=  \' current-cat-parent\';
        }
        $output .=  \' class="\' . $class . \'"\';
        $output .= ">$link\\n";
        $output .= "<img src=\'http://localhost/wp-content/themes/vom13/images/producten/";
        $output .= "$link2.png\'>\\n";
    } else {
        $output .= "\\t$link<br />\\n";
    }
}




}
正如你所见,它目前在文件夹中查找图像,但这绝对不是理想的,因为我不是唯一一个添加类别的人,但我不想给每个人FTP访问权限。

有什么解决方案吗?

谢谢

2 个回复
最合适的回答,由SO网友:mrwweb 整理而成

有一个很棒的插件叫做Taxonomy Images. 它允许您为每个类别设置一个图像,然后您可以通过各种方式访问该图像。

这有点奇怪,因为它使用一些自定义过滤器来返回图像,但文档非常好,您应该能够找到它。

==更新==这里有一个基于ID获取图像的快速示例(我假设您将$category 代码段中已有的对象:

$images = get_option(\'taxonomy_image_plugin\');
$cat_id = $category->term_taxonomy_id;
if( array_key_exists( $cat_id, $images ) ) {
    echo wp_get_attachment_image( $images[$cat_id] );
}
这是a slightly expanded version 支持论坛上的代码。

SO网友:Fanky

我认为最好的办法是allow tha admin choose the featured image 对于类别from Media Library. 这就是WooCommerce对产品类别所做的。我检查了WooCommerce是如何做到这一点的(在includes/admin/class-wc-admin-taxonomies.php中),下面是可重用代码(在functions.php或您的自定义插件中使用)。它可以与自定义帖子类型的分类一起使用。

<?php
/* Image thumbnail for category */

/* If this CPT, use scripts and styles to display Media Library popup */
function media_uploader() {
    global $post_type;
    if( \'YOUR_CUSTOM_POST_TYPE\' == $post_type) {
        if(function_exists(\'wp_enqueue_media\')) {
            wp_enqueue_media();
        }
        else {
            wp_enqueue_script(\'media-upload\');
            wp_enqueue_script(\'thickbox\');
            wp_enqueue_style(\'thickbox\');
        }
    }
}
add_action(\'admin_enqueue_scripts\', \'media_uploader\');

/*show the form field*/

add_action( \'YOUR_CUSTOM_POST_TYPE-category_add_form_fields\', \'add_image_field\', 10, 2 );
add_action( \'YOUR_CUSTOM_POST_TYPE-category_edit_form_fields\', \'add_image_field\', 10, 2 );
function add_image_field($taxonomy) {
    if(is_object($taxonomy)) // edit term not add term
    $selectedimgid = get_term_meta( $taxonomy->term_id, \'YOUR_CUSTOM_POST_TYPE_cat_thumbnail_id\', true );
    
    ?>
    <div class="form-field term-thumbnail-wrap">
        <label>Thumbnail</label>
        <div id="YOUR_CUSTOM_POST_TYPE_cat_thumbnail" style="float: left; margin-right: 10px;"><img src="<?php if(isset($selectedimgid)) echo wp_get_attachment_image_src($selectedimgid)[0]; else echo "PLACEHOLDER-IMAGE-HERE.jpg";?>" width="60px" height="60px" /></div>
        <div style="line-height: 60px;">
            <input type="hidden" id="YOUR_CUSTOM_POST_TYPE_cat_thumbnail_id" name="YOUR_CUSTOM_POST_TYPE_cat_thumbnail_id" value="<?php if(isset($selectedimgid)) echo $selectedimgid; ?>" />
            <button type="button" class="upload_image_button button">Upload/Add image</button>
            <button type="button" class="remove_image_button button">Remove image</button>
        </div>
        <script type="text/javascript">

            // Only show the "remove image" button when needed
            if ( ! jQuery( \'#YOUR_CUSTOM_POST_TYPE_cat_thumbnail_id\' ).val() ) {
                jQuery( \'.remove_image_button\' ).hide();
            }

            // Uploading files
            var file_frame;

            jQuery( document ).on( \'click\', \'.upload_image_button\', function( event ) {

                event.preventDefault();

                // If the media frame already exists, reopen it.
                if ( file_frame ) {
                    file_frame.open();
                    return;
                }

                // Create the media frame.
                file_frame = wp.media.frames.downloadable_file = wp.media({
                    title: \'Choose an image\',
                    button: {
                        text: \'Use image\'
                    },
                    multiple: false
                });

                // When an image is selected, run a callback.
                file_frame.on( \'select\', function() {
                    var attachment           = file_frame.state().get( \'selection\' ).first().toJSON();
                    var attachment_thumbnail = attachment.sizes.thumbnail || attachment.sizes.full;

                    jQuery( \'#YOUR_CUSTOM_POST_TYPE_cat_thumbnail_id\' ).val( attachment.id );
                    jQuery( \'#YOUR_CUSTOM_POST_TYPE_cat_thumbnail\' ).find( \'img\' ).attr( \'src\', attachment_thumbnail.url );
                    jQuery( \'.remove_image_button\' ).show();
                });

                // Finally, open the modal.
                file_frame.open();
            });

            jQuery( document ).on( \'click\', \'.remove_image_button\', function() {
                jQuery( \'#YOUR_CUSTOM_POST_TYPE_cat_thumbnail\' ).find( \'img\' ).attr( \'src\', \'PLACEHOLDER-IMAGE-HERE.jpg\' );
                jQuery( \'#YOUR_CUSTOM_POST_TYPE_cat_thumbnail_id\' ).val( \'\' );
                jQuery( \'.remove_image_button\' ).hide();
                return false;
            });

            jQuery( document ).ajaxComplete( function( event, request, options ) {
                if ( request && 4 === request.readyState && 200 === request.status
                    && options.data && 0 <= options.data.indexOf( \'action=add-tag\' ) ) {

                    var res = wpAjax.parseAjaxResponse( request.responseXML, \'ajax-response\' );
                    if ( ! res || res.errors ) {
                        return;
                    }
                    // Clear Thumbnail fields on submit
                    jQuery( \'#YOUR_CUSTOM_POST_TYPE_cat_thumbnail\' ).find( \'img\' ).attr( \'src\', \'PLACEHOLDER-IMAGE-HERE.jpg\' );
                    jQuery( \'#YOUR_CUSTOM_POST_TYPE_cat_thumbnail_id\' ).val( \'\' );
                    jQuery( \'.remove_image_button\' ).hide();
                    // Clear Display type field on submit
                    jQuery( \'#display_type\' ).val( \'\' );
                    return;
                }
            } );

        </script>
        <div class="clear"></div>
    </div>
    <?php
    
}



/* and save it */

add_action( \'edited_YOUR_CUSTOM_POST_TYPE-category\', \'YOUR_CUSTOM_POST_TYPE_extra_fields_save\', 10, 2);
add_action( \'created_YOUR_CUSTOM_POST_TYPE-category\', \'YOUR_CUSTOM_POST_TYPE_extra_fields_save\', 10, 2);
function YOUR_CUSTOM_POST_TYPE_extra_fields_save( $term_id ) {

    if ( !isset( $_POST[\'YOUR_CUSTOM_POST_TYPE_cat_thumbnail_id\'] ) ) return;
    update_term_meta( $term_id, "YOUR_CUSTOM_POST_TYPE_cat_thumbnail_id", $_POST[\'YOUR_CUSTOM_POST_TYPE_cat_thumbnail_id\'] );

}
?>
通往show 模板中的图像还将使用get_term_meta():

<?php
$termimgid = get_term_meta( $term->term_id, \'thamaterial_cat_thumbnail_id\', true );
if(isset($termimgid))
    echo \'<img alt="\'.$term->name.\'" src="\'.wp_get_attachment_image_src($termimgid)[0].\'" /> \'.$term->name;

结束

相关推荐

如何使用Walker_Nav_Menu排除/删除子菜单

我正在使用自定义菜单和扩展的Walker\\u Nav\\u菜单类在新站点上工作,以向wp Nav菜单添加自定义内容。我在wordpress admin的菜单中注册了一个自定义选择框(键:my\\u custom\\u menu),请看下图:我的问题是:如果用户使用自定义菜单而不是多级子菜单,是否有办法删除/排除子菜单,例如://conditional for parent menu item Contact us if($menu_item->my_custom_menu !== \'no