2012-08-29 26 views
0

現在完美無缺。更新的代碼。謝謝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"]); 
    } 
    } 

更新 code and bin。 http://pastebin.com/2ZWisprm

+0

你的意思是引用超全局'$ _POST'而不是'$ post'嗎? –

+0

是的,我想我的問題是相當微不足道的,熟練程序員真的很抱歉,並會使用您的鏈接作爲參考,但這個答案是現貨,並釋放了2天的搜索。 newfurniturey - 謝謝你。 – n2codes

回答

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是數組或其他非對象數據類型)。

+0

工程很漂亮,但現在正在拋出未定義的索引:第4行的website_url。我嘗試了以前的代碼,但它給出了錯誤消息並將錯誤代碼放入了url輸入框中。這就像鼴鼠w pop流行另一個彈出我不能贏得嘻嘻.. – n2codes

+0

我以前的答案應該在技術上適用於你的第4行;使用'isset($ custom ['website_url'])'會解決'undefined index'錯誤。如果你從不聲明'$ website_url',那麼'isset()'返回false,你的'<?php echo $ website_url; ?''會拋出一個關於未定義變量$ website_url的新錯誤''嘗試,而不是以前的答案:'$ website_url = isset($ custom ['website_url'])? $ custom ['website_url'] [0]:'';' – newfurniturey

+0

已更新到頂部。也只是想說謝謝。我意識到這需要寶貴的時間來幫助像我這樣的新手,並認真對待,我非常感激。儘管我在使用$ POST無法掌握如何實現它之前已經有了isset檢查的經驗。再次感謝。 – n2codes