我正在尝试用wordpress 4创建自定义帖子类型。我阅读了一本名为“从头开始构建Wordpress主题”的教程。我在本地主机上,因此调试模式处于启用状态。
当我尝试创建一个新的CPT时,我得到了表单,但仍然得到了undefined index errors on source, author and date.
我试图通过检查数组索引来解决这个问题,但这两个问题都没有解决。你能帮我解决这个问题吗?
function basin_manager_meta_options(){
global $post;
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $post_id;
$custom = get_post_custom($post->ID);
if(isset($custom["source"])){
$source= $custom["source"][0];
}
$author= $custom["author"][0];
$date= $custom["date"][0];
?>
<style type="text/css">
<?php include(\'basin-manager.css\'); ?>
</style>
<div class="basin_manager_extras">
<?php
$website= ($website == "") ? "http://" : $website;
?>
<div><label>Website / Gazete ? :</label>
<input name="source" value="<?php echo $source; ?>" />
</div>
<div><label>Yazar:</label>
<input name="author" value="<?php echo $author; ?>" />
</div>
<div><label>Date:</label>
<input name="date" value="<?php echo $date; ?>" />
</div>
<?php
}
add_action(\'save_post\', \'basin_manager_save_extras\');
function basin_manager_save_extras(){
global $post;
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ){
//if you remove this the sky will fall on your head.
return $post_id;
}else{
update_post_meta($post->ID, "source", $_POST["source"]);
update_post_meta($post->ID, "author", $_POST["author"]);
update_post_meta($post->ID, "date", $_POST["date"]);
}
}
谢谢。
最合适的回答,由SO网友:TTech IT Solutions 整理而成
我看到需要更改的第一件事是将未经检查的值分配给输入字段。在回显之前,请尝试将isset环绕变量,如果错误未分配或为空,则这将停止错误。
第二件事,因为您已经传递了一个post id来获取\\u post\\u custom(),所以不需要第二个数组。
function basin_manager_meta_options(){
global $post;
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $post_id;
$custom = get_post_custom($post->ID);
if(isset($custom["source"])){
$source= $custom["source"];
}
$author= $custom["author"];
$date= $custom["date"];**
?>
<style type="text/css">
<?php include(\'basin-manager.css\'); ?>
</style>
<div class="basin_manager_extras">
<?php
$website= ($website == "") ? "http://" : $website;
?>
<div><label>Website / Gazete ? :</label>
<input name="source" value="<?php echo isset($source) ? source : \'\'; ?>" />
</div>
<div><label>Yazar:</label>
<input name="author" value="<?php echo isset($author) ? $author : \'\'; ?>" />
</div>
<div><label>Date:</label>
<input name="date" value="<?php echo isset($date) ? $date : \'\'; ?>" />
</div>
<?php
}
add_action(\'save_post\', \'basin_manager_save_extras\');
function basin_manager_save_extras(){
global $post;
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ){
//if you remove this the sky will fall on your head.
return $post_id;
} else {
update_post_meta($post->ID, "source", $_POST["source"]);
update_post_meta($post->ID, "author", $_POST["author"]);
update_post_meta($post->ID, "date", $_POST["date"]);
}
}