WordPress.org

Forums

Categories Images
[resolved] Displaying Taxonomy Images within Post Editing (1 post)

  1. olyma
    Member
    Posted 2 years ago #

    I've been trying a bunch to get my taxonomy images to appear on my custom post type editing page -- as a part of the checkbox list, and I finally got it!!!!! Anyway, this feature/hack of this plugin might be quite nice and helpful for others to know how to do, too. In some instances, though not all, it's nice to have the images themselves appear on the post editing screen as a part of the checkbox list. So here's the code that seems to work for me:

    add_filter('wp_terms_checklist_args', 'display_my_imgs', 1, 2 );
    function display_my_imgs( $args, $post_id){
    	if ( 'my_custom_post_type' == get_post_type( $post_id ) && $args['taxonomy'] == 'my_custom_taxonomy' )
    	$args['walker'] = new my_imgs_walk;
    	return $args;
    }
    class my_imgs_walk extends Walker {
    	var $tree_type = 'category';
    	var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
    
    	function start_lvl( &$output, $depth = 0, $args = array() ) {
    		$indent = str_repeat("\t", $depth);
    		$output .= "$indent<ul class='children'>\n";
    	}
    
    	function end_lvl( &$output, $depth = 0, $args = array() ) {
    		$indent = str_repeat("\t", $depth);
    		$output .= "$indent</ul>\n";
    	}
    
    	function start_el( &$output, $category, $depth, $args, $id = 0 ) {
    		extract($args);
    		if ( empty($taxonomy) )
    			$taxonomy = 'category';
    
    		if ( $taxonomy == 'category' )
    			$name = 'post_category';
    		else
    			$name = 'tax_input['.$taxonomy.']';
    
    		$class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
    		$output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit">
    			<table border="0" cellspacing="0" cellpadding="0"><tr><td valign=center style="padding-right:5px;">
    				<input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' />
    			</td><td valign=center style="padding-right:5px;">
    				<div style="height:36px;width:36px;background-color:antiquewhite;">
    				<img style="height:36px;width:36px;" src="' . z_taxonomy_image_url($category->term_id) . '" />
    				</div>
    			</td><td valign=center>
    				' . esc_html( apply_filters('the_category', $category->name )) . '
    			</td></tr></table>
    				</label>';
    	}
    	function end_el( &$output, $category, $depth = 0, $args = array() ) {
    		$output .= "</li>\n";
    	}
    }

    The "wp_terms_checklist" I originally figured was where I should focus my efforts, but found that it was actually the <b>walker class</b> of the "wp_terms_checklist," controlled by the "wp_terms_checklist_args" that does it. Walker class is what pulls the list up for display, so I created a custom walker class for the checkbox list of the custom taxonomy. (That is a mouthful.)

    Essentially, in detail, what the code above does is, it calls up the walker class stuff for one's custom taxonomy within one's custom post type, and creates a new walker class for it's display within the custom post type's individual entry editing screen. Then within that walker class, I dropped in the code to display the taxonomy images one adds. After that, I put all the components (checkbox, image, and category name) in a table for easier centering and spacing. The div is to make it so that if there is no image for that category, that it won't be empty. All the CSS is coded into the above too, but if one wanted to extrude all the spacing and centerings into a separate CSS file, I'm sure it's possible.

    The above is also quite fine tuned for the specific images and project I'm working on, so you may need to adjust it for your specific application.

    If you're using the Categories Images plugin (for which this is written) which already has beautiful core functionality, this little extra coding added some admin functionality that I needed for my specific project. And now my images, with checkboxes, appear on my post editing page!!!!! Yay!!!!!! I hope others find this helpful if they have a similar application.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic