未定义的变量:发布

时间:2014-02-28 16:54:25

标签: php wordpress variables undefined

我正在调试我的wordpress主题,我一直在我的functions.php文件中收到此通知:

未定义变量:在第961行发布(myfolders)/functions.php

这是第961行

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

该代码用于我的自定义帖子类型(项目组合)项目描述的自定义项目。

导致变量未定义的原因是什么?以下是完整的代码:

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));

1 个答案:

答案 0 :(得分:1)

可以在函数内部使用post global,但是你没有在外面设置它。

变化:

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中输出此内容。你想要实现什么目标?