我正在处理一些自定义文章类型。我完成了第一个,并意识到我使用的metabox代码可以被其他,将来的自定义帖子类型(以及页面,帖子等)重新使用。所以我把这个类放在自己的PHP文件中,并将其包含在我的子主题的functions.php文件中。我以前从未使用过课程,但是我认为我可以随时从任何地方调用该课程,因为这是我为功能所做的。相反,我从我的post-types.php文件中调用它,并得到以下错误:从其他文件调用php类
“致命错误:类'My_meta_box'在D:\ helga \ xampp \ htdocs \ plagueround \ wp-content \ themes \ plagueround_new2 \功能\ - types.php后上线73"
在我的孩子主题的functions.php,我包括在该文件在我的生活类:
define('CHILDTHEME_DIRECTORY', get_stylesheet_directory() . '/');
// Meta Box Class - makes meta boxes
require_once(CHILDTHEME_DIRECTORY . 'functions/meta_box_class.php');
这里是我的类
//My Meta Box CLASS
//from: http://www.deluxeblogtips.com/2010/05/howto-meta-box-wordpress.html
class My_meta_box {
protected $_meta_box;
// create meta box based on given data
function __construct($meta_box) {
$this->_meta_box = $meta_box;
add_action('admin_menu', array(&$this, 'add'));
add_action('save_post', array(&$this, 'save'));
}
/// Add meta box for multiple post types
function add() {
foreach ($this->_meta_box['pages'] as $page) {
add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
}
}
// Callback function to show fields in meta box
function show() {
global $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($this->_meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
'<br />', $field['desc'];
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
'<br />', $field['desc'];
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
case 'select2': //for when value and display text don't match
//$options array must be multidimensional with both the 'value' and the 'text' for each option
//for example = array(array(text=>text1,value=>value1),array(text=>text2,value=>value2))
//then in <option> value = $option['value'] and text = $option['text']
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option value="',$option['value'],'"', $meta == $option['value'] ? ' selected="selected"' : '', '>', $option['desc'], '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '<td>',
'</tr>';
}
echo '</table>';
}
// Save data from meta box
function save($post_id) {
// verify nonce
if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($this->_meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
}
然后在我定义自定义类型后,我有一些元打我types.php后的文件:
$prefix = '_events_';
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$events_meta_box = array(
'id' => 'wpt_events',
'title' => 'Events Options',
'pages' => array('events'), // multiple post types possible
'context' => 'side',
'priority' => 'low',
'fields' => array(
array(
'name' => 'Headline',
'desc' => 'Enter the heading you\'d like to appear before the video',
'id' => $prefix . 'heading1',
'type' => 'text',
'std' => ''
),
)
);
$my_box = new My_meta_box($events_meta_box);
所以 - 我该如何引用类的其他文件?
EDITS/CONTINUATION
简单地移动放在require_once语句后types.php(一个试图调用类)似乎工作。但它赫克我试过自动加载脚本
function __autoload($className)
{
require(CHILDTHEME_DIRECTORY .'functions/class_' . $className . '.php') ;
}
,我改名为我的类文件class_My_meta_box.php和My_meta_box是类名
这将导致以下错误: 警告:要求( D:\ helga \ xampp \ htdocs \ plagueround/wp-content/themes/plagueround_new/functions/class_WP_User_Search.php)[function.require]:未能打开流:在D:\ helga \ xampp \ htdocs中没有这样的文件或目录\ plagueround \ wp-content \ themes \ plagueround_new \ functions.php on line 66
致命错误:require()[function.require]:Fail D:\ helga \ xampp \ htdocs \ plagueround/wp-content/themes/plagueround_new/functions/class_WP_User_Search.php'(include_path ='.; D:\ helga \ xampp \ php \ PEAR')在D: \ helga \ xampp \ htdocs \ plagueround \ wp-content \ themes \ plagueround_new \ functions.php 66行
我不完全明白它说什么,但我认为它试图自动加载一些默认的wordpress类。 ..并正在我的目录,显然不存在。
您的metabox.php没有被包括在内。你确定require_once语句加载正确吗? – 2010-08-06 17:52:04
@Byron:如果文件不存在,'require' /'require_once'会导致致命错误。与Helga不同的致命错误正在变得越来越严重。 – Powerlord 2010-08-06 18:00:20
该死的你都快。谢谢!以及我测试了带有jquery警报的require_once语句,所以我很确定它正在正确加载。它似乎工作时,我将require语句移动到post-types.php我在哪里引用类 – helgatheviking 2010-08-06 18:28:38