自定义帖子类型列奇怪问题

时间:2011-12-25 作者:Brigante

我的主题中有4种自定义帖子类型。当我为他们添加一个描述列(the\\u摘录)时,我得到了一些非常奇怪的行为。

描述列在每个自定义帖子类型的每个帖子上重复4次。还有一个我无法摆脱的PHP通知。

像这样:

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.
这是我的自定义帖子类型代码。除了职位类型名称之外,每个职位的代码都是相同的。

<?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;
        }
    }
?>
我不明白为什么“描述”列会重复多次,因为我有自定义的帖子类型。此外,我真的无法理解PHP的注意事项。

我正在LAMP服务器上进行本地开发,我的WP版本是3.3。

非常感谢您的帮助。谢谢

1 个回复
SO网友:Brigante

解决了!自WP版本3.1以来,用于向自定义帖子类型添加列的挂钩发生了变化。

在3.1之前:

add_action("manage_posts_custom_column",  "testimonials_custom_columns");
3.1之后,您必须使用:

add_action("manage_testimonials_posts_custom_column",  "testimonials_custom_columns");
因此,必须在manage\\u post\\u custom\\u列挂钩中指定自定义帖子类型名称。

希望这个答案能节省一些时间。

结束

相关推荐