Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have 4 custom post types in my theme. When I add a description column (the_excerpt) for them I get some really strange behavior.

The description column repeats 4 times on each post on every custom post type. Also get a PHP notice that I can't get rid of.

Like this:

Notice: Trying to get property of non-object in H:\htdocs\wp-content\themes\mytheme\admin_includes\other\excerpt.php on line 10
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Notice: Trying to get property of non-object in H:\htdocs\wp-content\themes\mytheme\admin_includes\other\excerpt.php on line 10
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Notice: Trying to get property of non-object in H:\htdocs\wp-content\themes\mytheme\admin_includes\other\excerpt.php on line 10
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Notice: Trying to get property of non-object in H:\htdocs\wp-content\themes\mytheme\admin_includes\other\excerpt.php on line 10
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Here's my code for the custom post type. The code is the same for each one apart from the post type name.

<?php
    add_action('init', 'testimonials_register');
    function testimonials_register() {
        $labels = array(
            'name' => _x('Testimonials', 'post type general name'),
            'singular_name' => _x('Testimonial', 'post type singular name'),
            'add_new' => _x('Add New', 'Testimonial'),
            'add_new_item' => __('Add New Testimonial'),
            'edit_item' => __('Edit Testimonial'),
            'new_item' => __('New Testimonial'),
            'view_item' => __('View Testimonial'),
            'search_items' => __('Search Testimonials'),
            'not_found' =>  __('Nothing found'),
            'not_found_in_trash' => __('Nothing found in Trash'),
            'parent_item_colon' => ''
        );
        $args = array(
            'labels' => $labels,
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'menu_icon' => get_stylesheet_directory_uri() . '/images/icons/testimonials-admin.png',
            'rewrite' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'menu_position' => null,
            'rewrite' => array(
                'slug' => 'testimonials',
                'with_front' => FALSE,
            ),
            'supports' => array('title','editor','thumbnail')
        );
        register_post_type( 'testimonials' , $args );
    }

    add_action("manage_posts_custom_column",  "testimonials_custom_columns");
    add_filter("manage_edit-testimonials_columns", "testimonials_edit_columns");

    function testimonials_edit_columns($columns){
        $columns = array(
            "cb" => "<input type=\"checkbox\" />",
            "title" => "Testimonial Title",
            "description" => "Description"
        );
        return $columns;
    }

    function testimonials_custom_columns($column){
        global $post;
        switch ($column)
        {
            case "description":
                the_excerpt();
                break;
        }
    }
?>

I can't see why the description column would repeat for as many times as I have custom post types. Also I really can't figure out the PHP notice.

I'm developing locally on LAMP server and my WP version is 3.3.

Would really appreciate some help. Thanks!

share|improve this question
    
Please ignore the PHP notice, that was caused by a custom excerpt function I had. Sorry about that! The description column still repeats 4 times though. –  Johannes Dec 25 '11 at 0:59
    
could you give the line of code that the error is describing? –  Manny Fleurmond Dec 25 '11 at 1:21
    
You're redefining the columns array in the testimonials_edit_columns function instead of adding to it, is that intentional? –  t31os Dec 25 '11 at 1:24
    
@Manny Fleurmond The error is gone now. It was just a missing global post. The main issue still remains though that the_excerpt is repeated 4 times for each post in the description column. –  Johannes Dec 25 '11 at 1:26
    
@t31os thanks for the comment. I've just copied that from a tutorial. How would you suggest I do it instead? –  Johannes Dec 25 '11 at 1:47
add comment

1 Answer

Worked it out! Since WP version 3.1 the hook for adding columns to custom post types has changed.

Before 3.1 it was:

add_action("manage_posts_custom_column",  "testimonials_custom_columns");

After 3.1 you have to use this:

add_action("manage_testimonials_posts_custom_column",  "testimonials_custom_columns");

So you have to specify the custom post type name in the manage_post_custom_column hook.

Hopefully this answer will save someone some time.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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