2014-02-28 30 views
0

我調試我的wordpress主題,我一直在我functions.php文件得到這個通知:未定義的變量:在後

未定義的變量:在後(myfolders)/functions.php上線961

這是線961

echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true)); 

的代碼是爲我的自定義後類型(組合)項目描述定製TinyMCE的。

什麼導致變量未定義?下面是完整的代碼:

add_action('add_meta_boxes', 'mpc_add_description_meta_box'); 
// Add the projects Meta Box 
function mpc_add_description_meta_box() { 
    add_meta_box('mpc_add_description_meta_box', 'Project Description', 'mpc_add_description_meta_box_callback', 'portfolio', 'normal', 'high'); 
} 

// the description output 
function mpc_add_description_meta_box_callback() { 
    global $post; 
    // Noncename needed to verify where the data originated 
    echo '<input type="hidden" name="mpc_projects_description_noncename" id="mpc_projects_description_noncename" value="'.wp_create_nonce(plugin_basename(__FILE__)).'" />'; 
    // Get the location data if its already been entered 
    $input = get_post_meta($post->ID, 'mpc_projects_description', true); 
    // echo '<input type="text" name="mpc_projects_description" value="' . $input . '" " />'; 
    wp_editor($input, 'mpc_projects_description', array('textarea_name' => 'mpc_projects_description', 'editor_css' => '<style>#wp-mpc_projects_description-editor-container{background-color:white;style="width:100%;}</style>')); 

    echo '<table id="post-status-info" cellspacing="0"><tbody><tr><td id="wp-word-count">Word count: <span class="word-count_2">'; 
    $words = strip_tags($input); 
    $count = str_word_count($words, 0); 
    echo $count; 
    echo '</span></td> 
    <td class="autosave-info"> 
    <span class="autosave-message">&nbsp;</span> 
    </td> 
    </tr></tbody></table>'; 
} 

//save the data 
add_action('save_post', 'mpc_save_description_meta', 1, 2); // save the custom fields 
function mpc_save_description_meta($post_id, $post) { 
    // verify this came from the our screen and with proper authorization, 
    // because save_post can be triggered at other times 
    if (!wp_verify_nonce($_POST['mpc_projects_description_noncename'], plugin_basename(__FILE__))) { 
    return $post->ID; 
    } 
    // Is the user allowed to edit the post or page? 
    if (!current_user_can('edit_post', $post->ID)) 
     return $post->ID; 
    // OK, we're authenticated: we need to find and save the data 
    // We'll put it into an array to make it easier to loop though. 
    $mpc_description_meta['mpc_projects_description'] = $_POST['mpc_projects_description']; 
    // Add values of $mpc_description_meta as custom fields 
    foreach ($mpc_description_meta as $key => $value) { // Cycle through the $mpc_description_meta array! 
     if($post->post_type == 'revision') return; // Don't store custom data twice 
     $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely) 
     if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value 
      update_post_meta($post->ID, $key, $value); 
     } else { // If the custom field doesn't have a value 
      add_post_meta($post->ID, $key, $value); 
     } 
     if(!$value) delete_post_meta($post->ID, $key); // Delete if blank 
    } 
} 



echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true)); 
+0

如果你使用'get_post_meta($ post-> ID',變量$ post'沒有任何東西(比如ID),你需要看看你是否能找到應該設置的位置。有可能是一個錯誤,例如,如果它正在爲已登錄的用戶設置,但不是爲未登錄的用戶設置的。類似的事情。 – Quixrick

+0

對不起@Quixrick但我不知道當你說變量$ post沒有任何東西時,我不明白你是否需要用某些東西替換「ID」 – Breon

+0

不會。你得到的錯誤是因爲你試圖傳遞'$ post-> ID'作爲一個參數,唯一的是你沒有'$ post-> ID' set,所以它會拋出錯誤,因爲你試圖傳遞一些不存在的東西,'$ post'應該有東西在裏面,比如'ID'。 – Quixrick

回答

1

全局函數在函數內部可用,但您沒有在外部設置它。

變化:

echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true)); 

要:

global $post; 
echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true)); 

這也可能取決於您使用此代碼。如果你在post對象設置之前觸發代碼,那麼你會得到這個錯誤。

如果它只是在functions.php中,然後將其更改爲:

function wpse_post_test() { 
    global $post; 
    echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true)); 
} 
add_action('wp', 'wpse_post_test'); 

再次審查您的問題看來你正試圖輸出這裏面的functions.php後。你想達到什麼目的?

+0

嗨Nathan。是的,我h在我的functions.php文件中添加了這段代碼。我有一個稱爲投資組合的自定義帖子類型,對於每篇文章,我都試圖爲我的項目描述創建額外的tinymce。 – Breon

+0

我試過你的第二個解決方案,它擺脫了Undefined變量,但它將我的內容添加到我在頁面上調用該區域的tinymce中,但也在屏幕的頂部。 – Breon

+0

當你將它插入到functions.php中時,你期望發生什麼?如果它顯示在正確的區域以及屏幕頂部,則完全刪除該新功能並解決問題。 –