现在完美无瑕地工作。更新的代码。谢谢newfurniturey。
function foo_options(){
global $post;
if (isset($custom['website_url']) && isset($custom['website_url'][0])) {
$website_url = isset($custom['website_url']) ? $custom['website_url'][0] : '';}
?>
<div id="foo-options">
<label>Website URL:</label><input name="website_url" value="<?php echo $website_url; ?>" />
</div><!--end foo-options-->
<?php
}
{
function update_website_url(){
global $post;
if (($post != null) && isset($_POST['website_url'])) {
update_post_meta($post->ID, "website_url", $_POST["website_url"]);
}
}
更新了代码和bin。 http://pastebin.com/2ZWisprm
答案 0 :(得分:1)
要按顺序解决问题,请参阅以下一行:
update_post_meta($post->ID, "website_url", $_POST["website_url"]);
trying to access a property of a non-object
适用于$post->ID
。发生此错误的唯一方法是$post
不是对象(例如数组),或者是null
。
undefined index
错误适用于$_POST["website_url"]
;当您的表单当前未被POST,或者当前POST数据中不存在字段名时,就会发生这种情况。
我不知道你的$post
变量中应该包含哪个对象,所以以下只是猜测,但请尝试以下更新:
function update_website_url(){
global $post;
if (($post != null) && isset($_POST['website_url'])) {
update_post_meta($post->ID, "website_url", $_POST["website_url"]);
}
}
这将确保$post
不为空,并假设它是一个正确的对象,并且已设置website_url
索引。您可能希望增加检查以使用!empty()
而不是isset()
,但上述内容应解决您的错误(除非$post
是数组或其他非对象数据类型)。< / p>