自定义字段未保存在最新的WordPress 3.0.1中

时间:2010-10-27 作者:Shaun

我在几周前就开始使用它了,我唯一能想到的是WordPress已经更新并破坏了它的功能?

我按照此处的教程添加自定义字段:http://net.tutsplus.com/tutorials/wordpress/rock-solid-wordpress-3-0-themes-using-custom-post-types/

它工作得很好(我输入了大约50篇自定义帖子,都带有自定义字段),但当我今天去编辑它时,我对自定义字段所做的任何更改都不会被保存,尽管其他更改工作得很好。

有人知道这是不是一个已知的错误吗?

Edit: 下面是在函数中添加的代码。php到全新安装(主题:twentyten)

add_action(\'init\', \'testimonials_register\');
add_action("admin_init", "admin_init");
add_action(\'save_post\', \'save_testimonial\');




function testimonials_register() {
    $args = array(
        \'label\' => __(\'Testimonials\'),
        \'singular_label\' => __(\'Testimonial\'),
        \'public\' => true,
        \'show_ui\' => true,
        \'capability_type\' => \'post\',
        \'menu_position\' => 5,
        \'hierarchical\' => false,
        \'rewrite\' => true,
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\',\'custom-fields\', \'revisions\', \'excerpt\', \'page-attributes\')
    );

    register_post_type( \'testimonials\' , $args );
}




register_taxonomy( \'testimonial_project_type\', array("testimonials") , array( \'hierarchical\' => true, \'label\' => \'Project Type\', \'query_var\' => true, \'rewrite\' => true ) );



function admin_init(){
    add_meta_box("testimonialInfo-meta", "Testimonial Options", "meta_options_testimonial", "testimonials", "advanced", "high");

}



function meta_options_testimonial(){
    global $post;
    $custom = get_post_custom($post->ID);
    $name = $custom["name"][0];
    $position = $custom["position"][0];
    $project_url = $custom["project_url"][0];
    $website = $custom["website"][0];
    ?>
  <label for="name" style="width:90px;display:inline-block">Name:</label> <input size="50" name="name" id="name" value="<?php echo $name; ?>" /><br>
  <label for="position" style="width:90px;display:inline-block">Position:</label> <input size="50" name="position" id="position" value="<?php echo $position; ?>" /><br>
  <label for="website" style="width:90px;display:inline-block">Website Name:</label> <input size="50" name="website" id="website" value="<?php echo $website; ?>" /><br>
  <label for="project_url" style="width:90px;display:inline-block">Project slug:</label> <input size="50" name="project_url" id="project_url" value="<?php echo $project_url; ?>" /> <small>E.g. \'parker-harris\'</small><br>
    <?php
}

    function save_testimonial(){
$custom_meta_fields = array( \'project_url\',\'name\',\'position\',\'website\');
foreach( $custom_meta_fields as $custom_meta_field ):
    if(isset($_POST[$custom_meta_field]) && $_POST[$custom_meta_field] != ""):
        update_post_meta($post->ID, $custom_meta_field, $_POST[$custom_meta_field]);
    endif;
endforeach;


    }








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

    function testimonials_edit_columns($columns){  
    $columns = array(  
        "cb" => "<input type=\\"checkbox\\" />",  
        "title" => "Testimonial Title",  
        "name" => "Name",  
        "description" => "Excerpt",  
        "project_url" => "Project Slug"
    );  

    return $columns;  
   }  

    function testimonials_custom_columns($column){  
    global $post;  
    switch ($column)  
    {  
        case "name":  
            $custom = get_post_custom();  
            echo $custom["name"][0].", ".$custom["position"][0]."<br> ".$custom["website"][0];
            break;  
        case "description":  
            the_excerpt(); 
            break;  
        case "project_url":  
            $custom = get_post_custom();  
            echo "<a target=\'_blank\' href=\'/portfolio/".$custom["project_url"][0]."\'>".$custom["project_url"][0]."</a>";
            break;  
    }  
    }  









    // This shows testimonials in blog and feed.

    add_filter( \'pre_get_posts\', \'my_get_posts\' );

    function my_get_posts( $query ) {

if ( is_home() && false == $query->query_vars[\'suppress_filters\']  || is_feed() )
    $query->set( \'post_type\', array( \'post\', \'testimonials\') );

return $query;
    }

1 个回复
最合适的回答,由SO网友:Jan Fabry 整理而成

我将您的代码放入插件中,必须更改两件事才能使其正常工作:

英寸save_testimonial(), 你用$post 但不要将其声明为全球。所以$post->ID 将为空,并且update_post_meta() 不知道要保存什么。添加global $post; 在函数的开头

  • register_taxonomy() 不在您的init 并且可能调用得太早(如果它是一个插件,那么在functions.php). 将其移动到testimonals_register() 功能WP_DEBUG 设置为TRUE 你调试它的时候?那么你应该已经得到了引导我找到这个解决方案的警告。

  • 结束

    相关推荐