4

I have another dumb question, but I can't get one thing :)

I've found this very good article on creating custom post types:

http://thinkvitamin.com/code/create-your-first-wordpress-custom-post-type/

I'm not sure how Step 4 works.

This guys writes:

add_action("manage_posts_custom_column",  "portfolio_custom_columns");
add_filter("manage_edit-portfolio_columns", "portfolio_edit_columns");

function portfolio_edit_columns($columns){
  $columns = array(
    "cb" => "<input type=\"checkbox\" />",
    "title" => "Portfolio Title",
    "description" => "Description",
    "year" => "Year Completed",
    "skills" => "Skills",
  );

  return $columns;
}
function portfolio_custom_columns($column){
  global $post;

  switch ($column) {
    case "description":
      the_excerpt();
      break;
    case "year":
      $custom = get_post_custom();
      echo $custom["year_completed"][0];
      break;
    case "skills":
      echo get_the_term_list($post->ID, 'Skills', '', ', ','');
      break;
  }
}

I have two different post types ('books' and 'movies').

And I can't get how to link this code with right one!

I'm sure I'm missing something (most likely in the code), but I didn't see him including "portfolio_edit_columns" anywhere in the code.

I have found this in WP Codex: http://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column and it seems like

manage_edit-${post_type}_columns

does the magic, but I've tried both manage_edit-books_columns and manage_edit-movies_columns and nothing! :)

So how to create two separate column layouts for different post types?

3 Answers 3

4

The code from ThinkVitamin is right. I think the problem came from else where in your code.

Actually, the hook manage_edit-${post_type}_columns takes an argument $columns which is an array of all registered columns. To add a new column, just add a new element to this array, like this:

add_filter('manage_edit-film_columns', 'my_columns');
function my_columns($columns) {
    $columns['views'] = 'Views';
    return $columns;
}
3
  • I know how to add columns, I don't know how to display some of them for custom post type movies and some for custom post type books. And that's what the question is about. Mar 10, 2011 at 11:49
  • 1
    Oh, I saw you said "I've tried both manage_edit-books_columns and manage_edit-movies_columns and nothing" and I thought you had problem with adding columns. Anyway, the link I provided has a good example with explanation how to show content in columns, you should read it.
    – Anh Tran
    Mar 10, 2011 at 13:53
  • I still don't get that. I'm using two separate filters but it doesn't help at all. Mar 11, 2011 at 19:53
4

It´s even easier than you belive.

You have this two functions on wp:

manage_posts_columns (notice the "posts" part)

and

manage_posts_custom_column (again "posts")

You have same thing for pages, "manage_page_posts_columns" and "manage_page_posts_custom_column" (notice the "page_posts" part)

So, if you need to add those cols for x post type, you only need to do like this, le´t say your custom post type name is "movie", then:

manage_movie_posts_columns()
manage_movie_posts_custom_column()

Notice the "movie_posts" part, that´s why you see something like "manage_{custom_type}_posts_columns" on codex pages.

For a "books" custom type:

manage_books_posts_columns()
manage_books_posts_custom_column()

And so on... got it?

2

Take a look at how the rewrite $arg for your custom post type is.

To get a better view of the output data, just do something like

echo '<pre>';   
print_r($custom_post_type_obj);   
echo '</pre>';   

on the post type object when querying it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.