Adding Columns to the WP Edit Pages

A couple weeks ago I released the Portfolio Press, which uses custom post types to display images and portfolio information. Since the featured image is a key component of the post type, I wanted a way to display that from the post dashboard- along with the title, taxonomy, author, comments, and post date.

Update: This is now all in the Portfolio Post Type plugin, available on WordPress.org, which is incorporated into the theme.



I couldn’t find any code snippets available that did exactly what I wanted, but I did get close with WP Engineer’s post about adding thumbnails to post and pages and Shiba’s post about adding custom post type columns.

You’re welcome to download the Portfolio Post Type plugin and see how all the code was included. The code below is just the specific part about adding columns to edit screen.

Note, I already have a custom post type “portfolio” built. If you wanted to use these columns with a “art” post type for instance, you would need to swap out the name.

//  Add Columns to Portfolio Edit Screen
add_filter("manage_edit-portfolio_columns", "portfolio_edit_columns");
add_action("manage_posts_custom_column",  "portfolio_columns_display", 10, 2);
add_theme_support( 'post-thumbnails', array( 'portfolio' ) );
function portfolio_edit_columns($portfolio_columns){
	$portfolio_columns = array(
		"cb" => "<input type="\&quot;checkbox\&quot;" />",
		"title" => _x('Title', 'column name'),
		"thumbnail" => __('Thumbnail'),
		"portfolio-tags" => __('Tags'),
		"author" => __('Author'),
		"comments" => __('Comments'),
		"date" => __('Date'),
	);
	$portfolio_columns['comments'] = '</pre>
<div class="vers"><img src="' . esc_url( admin_url( 'images/comment-grey-bubble.png' ) ) . '" alt="Comments" /></div>
<pre>
';
	return $portfolio_columns;
}
function portfolio_columns_display($portfolio_columns, $post_id){
	switch ($portfolio_columns)
	{
		// Code from: http://wpengineer.com/display-post-thumbnail-post-page-overview
		case "thumbnail":
			$width = (int) 35;
			$height = (int) 35;
				$thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
				// image from gallery
				$attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
				if ($thumbnail_id)
					$thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
				elseif ($attachments) {
					foreach ( $attachments as $attachment_id => $attachment ) {
						$thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
					}
				}
					if ( isset($thumb) && $thumb ) {
						echo $thumb;
					} else {
						echo __('None');
					}
			break;
		case "portfolio-tags":
			if ($tag_list = get_the_term_list( $post->ID, 'portfolio-tags', '', ', ', '' ) ) {
				echo $tag_list;
			} else {
					echo __('None');
				}
			break;
	}
}

About Devin

I'm a WordPress developer based in Austin, Texas. Follow my projects on GitHub, or more general WordPress ramblings as @devinsays on twitter.

10 thoughts on “Adding Columns to the WP Edit Pages

  1. Freddie wrote:

    Hi,

    I’m trying to implement the image column as you have documented here, but I’m running into a strange problem.

    The column is added just fine for my custom post type, and images show up. The problem is that all of the images in the column are the same. Not only that, they’re the same image from one of my regular posts, not the custom post type…

    So I have my regular posts and a custom post type called “event”. In the “event’ page the column shows up, but they all display an image from a “post”.

    • Devin wrote:

      Maybe the query isn’t happening right. Try downloading the portfolio theme I built and cross check the code: http://wordpress.org/extend/themes/portfolio-press. It has a column view that displays the images of a custom post type and definitely works.

  2. Petros wrote:

    Hello
    I want to thank you VERY much
    big hugs Devin, this code made my day!

  3. JHouse wrote:

    This worked pretty well for me, thank you.
    I want to use custom columns, such as Price, too. Is there a simple way to extract data from my “price” custom field into said Price column?

    Thanks again for the nice tut.

    • Devin wrote:

      Yes, should be very similar. echo get_post_meta( $post_id, ‘price’, true ) in the column instead of the image/tags.

  4. Anton wrote:

    By default in “posts” post type, when we clicking on any category in categories column, on all posts page remain only posts related to this category. In your way we will go to unknown page, so better to display post type taxonomy like this:

    $terms = get_the_terms($post_id, ‘{TAXONOMY}’);
    if ( !empty($terms) ) {
    foreach ( $terms as $term )
    $post_terms[] = “slug.”‘> “.$term->name. ““;
    echo join( ‘, ‘, $post_terms );
    } else {
    echo _(‘None’);
    }

    • Devin wrote:

      Thanks. I’ll test it when I have a minute and update the post.

  5. Roberta wrote:

    I have tried twice installing Portfolio Theme, but I’m not getting the Portfolio panel in my dashboard…therefore, no usable Portfolio building can be done.

    I am trying to use this for a woodturning web site. I’m on a mac, and am hosted at Bluehost.

      • Roberta wrote:

        Thanks for your really quick reply….I kept reading, and found that plugin requirement, so got it going just fine. But maybe someday you’ll update your demo. I love the possibilities with this theme.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>