簡體   English   中英

未定義的變量:發布於

[英]Undefined variable: post in

我正在調試我的wordpress主題,並在我的functions.php文件中不斷收到此通知:

未定義的變量:在第961行的(myfolders)/functions.php中發布

這是961行

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

該代碼用於我的自定義帖子類型(Portfolio)項目描述的自定義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));

全局變量post在函數內部可用,但您尚未在外部設置。

更改:

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中輸出此內容。 您想達到什么目的?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM