我在函数末尾添加以下代码。php文件,用于根据自定义帖子的帖子标题填充分类法。问题是,当我添加代码时,尝试登录wp admin时会出现以下错误。非常感谢您能帮助我们弄清楚为什么会发生这种情况。
Error:
错误:由于意外输出,Cookie被阻止。有关帮助,请参阅此文档或尝试支持论坛。
Code:
<?php
function update_custom_terms($post_id) {
// only update terms if it\'s a \'artist\' post
if ( \'artist\' != get_post_type($post_id)) {
return;
// don\'t create or update terms for system generated posts
if (get_post_status($post_id) == \'auto-draft\') {
return;
}
$term_title = get_the_title($post_id);
$term_slug = get_post( $post_id )->post_name;
/*
* Check if a corresponding term already exists by comparing
* the post ID to all existing term descriptions.
\'artists\' is the taxonomy getting populated
*/
$existing_terms = get_terms(\'artists\', array(\'hide_empty\' => false));
foreach($existing_terms as $term) {
if ($term->description == $post_id) {
//term already exists, so update it and we\'re done
wp_update_term($term->term_id, \'artists\', array(
\'name\' => $term_title,
\'slug\' => $term_slug
)
);
return;
}
}
/*
* If we didn\'t find a match above, this is a new post,
* so create a new term.
*/
wp_insert_term($term_title, \'artists\', array(
\'slug\' => $term_slug,
\'description\' => $post_id
)
);
}
//run the update function whenever a post is created or edited
add_action(\'save_post\', \'update_custom_terms\');
function delete_term( $post_id ) {
$post = get_post( $post_id );
if ( term exists( $post->post_title, \'artists\' ) ) {
$term = get_term_by( \'name\', $post->post_title, \'artists\' );
wp_delete_term( $term->term_id, \'artists\' );
}
}
add_action( \'before_delete_post\', \'delete_term\' );
?>