我調試我的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"> </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));
如果你使用'get_post_meta($ post-> ID',變量$ post'沒有任何東西(比如ID),你需要看看你是否能找到應該設置的位置。有可能是一個錯誤,例如,如果它正在爲已登錄的用戶設置,但不是爲未登錄的用戶設置的。類似的事情。 – Quixrick
對不起@Quixrick但我不知道當你說變量$ post沒有任何東西時,我不明白你是否需要用某些東西替換「ID」 – Breon
不會。你得到的錯誤是因爲你試圖傳遞'$ post-> ID'作爲一個參數,唯一的是你沒有'$ post-> ID' set,所以它會拋出錯誤,因爲你試圖傳遞一些不存在的東西,'$ post'應該有東西在裏面,比如'ID'。 – Quixrick