我为一个wordpress网站开发了一个主题和一些插件。插件都是自定义的帖子类型。我试着搜索了几个小时的解决方案,但我找到的所有解决方案似乎都不适合我。许多解决方案表明,需要调用数组foreach参数工作,但我相当肯定,我的插件已经准确地使用了数组。我在本地开发人员上创建了一个完全工作的网站,没有任何错误,但是当我在网站的活动子目录(用于演示)上安装任何插件时,我得到以下三个错误:
-
"在/home3/adamshap/public_html/usdbls/wp-includes/post.php中为foreach()提供的无效参数"
-
"无法修改报头信息-报头已经由(output started at/home3/adamshap/public_html/usdbls/wp-includes/post.php:1468)在/home3/adamshap/public_html/usdbls/wp-includes/option.php在787行"
-
"无法修改报头信息-报头已经由(output started at/home3/adamshap/public_html/usdbls/wp-includes/post.php:1468)发送到/home3/adamshap/public_html/usdbls/wp-includes/option.php的788行"
额外的信息:我只收到错误时,上传我的自定义post_type插件(不是我开发的主题,只是插件)。错误出现在我所有的插件,而不是任何一个。我知道错误在于我所做的插件,所以我需要找出如何修复我的插件,而不是wordpress核心文件。
第一个错误指向wp-includes/post.php的第1468行。对应的代码段如下:
if ( $args->register_meta_box_cb )
add_action( 'add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1 );
$args->labels = get_post_type_labels( $args );
$args->label = $args->labels->name;
$wp_post_types[ $post_type ] = $args;
add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
foreach ( $args->taxonomies as $taxonomy ) {
register_taxonomy_for_object_type( $taxonomy, $post_type );
}
第二个和第三个错误指向wp-includes/option.php文件中的这段代码:
// The cookie is not set in the current browser or the saved value is newer.
$secure = ( 'https' === parse_url( site_url(), PHP_URL_SCHEME ) );
setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
$_COOKIE['wp-settings-' . $user_id] = $settings;
看到这两个文件都是wordpress核心文件,我相信错误实际上是在我的插件代码,而不是上面的片段。下面的dropbox链接链接到我完成的插件之一的txt文件(它超过了stackoverflow的字符限制)。
https://www.dropbox.com/s/137djd6x13wp2e5/about-intro.txt?dl=0提前感谢您的帮助!
解决方案找到了,部分感谢用户Marc B在上面的评论。
我们发现,在这三个错误中,真正的错误是第一个错误,另外两个错误是由第一个错误引起的。
我的第一个解决方案是更改wp-includes/post.php文件(第1468 - 1470行):
foreach ( $args->taxonomies as $taxonomy ) {
register_taxonomy_for_object_type( $taxonomy, $post_type );
}
:
if (is_array($taxonomy) || is_object($taxonomy)) {
foreach ($args ->taxonomies as $taxonomy) {
register_taxonomy_for_object_type($taxonomy, $post_type);
}
}
虽然这解决了我的问题,这是一个糟糕的解决方案,因为它需要编辑wp核心文件。在Marc B建议使用var_dump并找到我的插件中使用taxonomy数组(不正确)的部分之后,我找到了真正的解决方案:
我的插件注册了一个自定义post_type,原始代码如下:
$args = array(
'label' => __( 'Statement', 'text_domain' ),
'description' => __( 'About Statement', 'text_domain' ),
'labels' => $labels,
'supports' => array('',''),
'taxonomies' => false,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'query_var' => 'about',
'capability_type' => 'page',
);
register_post_type( 'about_type', $args );
}
add_action( 'init', 'about_post_type', 0);
可以看到,$args数组将'taxonomies'定义为'false'。这就是导致错误的原因。解决方案是简单地将值"false"更改为数组。固定函数如下:
$args = array(
'label' => __( 'Statement', 'text_domain' ),
'description' => __( 'About Statement', 'text_domain' ),
'labels' => $labels,
'supports' => array('',''),
'taxonomies' => array(),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'query_var' => 'about',
'capability_type' => 'page',
);
当我自己寻找答案时,我发现很多人似乎都有这个问题。如果您正在寻找类似的解决方案,我建议您查看插件的register_post_type函数,看看数组是如何处理分类法的。