解决WordPress短代码中Cannot redeclare function致命错误
问题解决:WordPress短代码函数重复声明致命错误
错误原因
Cannot redeclare get_child_categories() 错误的核心是:你的三个自定义函数(get_child_categories、get_posts_for_lastchild、list_categories)在页面保存或短代码调用时被重复定义。因为get_template_part会直接引入目标文件,当文件被多次加载(比如短代码重复调用、页面保存时的二次加载),就会触发函数重定义的致命错误。
修复方案
给每个函数添加存在性检查,确保只有函数未定义时才声明;同时移除文件末尾的直接echo,让短代码通过输出缓冲统一处理输出。
修改后的masterguide-categorylist.php代码
<?php if ( ! function_exists( 'get_child_categories' ) ) { function get_child_categories( $parent_category_id ){ $html = ''; $child_categories = get_categories( array( 'parent' => $parent_category_id, 'hide_empty' => true, 'taxonomy' => 'masterguidecategory' ) ); if( !empty( $child_categories ) ){ $html .= '<ul class="children">'; foreach ( $child_categories as $child_category ) { $html .= '<li class="child">'.$child_category->name; $child_categories = get_term_children($child_category->term_id, 'masterguidecategory'); if(!empty($child_categories)){ $html .= get_child_categories( $child_category->term_id ); }else{ $html .= get_posts_for_lastchild($child_category->term_id); } $html .= '</li>'; } $html .= '</ul>'; } return $html; } } if ( ! function_exists( 'get_posts_for_lastchild' ) ) { function get_posts_for_lastchild($category_id ){ $html = ''; // 修复未初始化变量的问题 $args = array('post_type' => 'masterguide','posts_per_page'=>'-1', 'orderby' => 'title', 'order' => 'ASC', 'tax_query' => array(array('taxonomy' => 'masterguidecategory' ,'field' => 'term_id', 'terms' => $category_id))); $postquery = new WP_Query( $args ); if($postquery->have_posts()) : $html .= '<ul class="children ChartPostList">'; while ($postquery->have_posts()) : $postquery->the_post(); $html .= '<li class="ChartPostListItem"><a class="button small" href='.get_the_permalink().'>'.get_the_title().'</a></li>'; endwhile; $html .= '</ul>'; endif; wp_reset_postdata(); // 重置查询,避免污染全局循环 return $html; } } if ( ! function_exists( 'list_categories' ) ) { function list_categories(){ $html = ''; $parent_categories = get_categories( array( 'parent' => 0, 'hide_empty' => true, 'taxonomy' => 'masterguidecategory' ) ); $html.= '<ul class="chartAccordion">'; foreach ( $parent_categories as $parent_category ) { $html .= '<li class="parent">'.$parent_category->name; $child_categories = get_term_children($parent_category->term_id, 'masterguidecategory'); if(!empty($child_categories)){ $html .= get_child_categories( $parent_category->term_id ); }else{ $html .= get_posts_for_lastchild($parent_category->term_id); } $html .= '</li>'; } $html.= '</ul>'; return $html; } } echo list_categories(); ?>
注:额外修复了get_posts_for_lastchild中$html未初始化的问题,添加wp_reset_postdata()避免全局查询异常。
functions.php代码保持不变
function mg_catlist_function(){ ob_start(); get_template_part( 'content/masterguide', 'categorylist' ); $content = ob_get_clean(); return $content; } add_shortcode('mg_categories','mg_catlist_function');
关键说明
! function_exists( '函数名' ):确保函数仅被定义一次,即使文件被重复加载也不会触发重定义错误。- 移除直接
echo(原代码中保留是因为短代码用ob缓冲捕获输出,无需额外修改):短代码通过输出缓冲统一获取内容,避免内容提前输出到页面头部。 wp_reset_postdata():自定义WP_Query后必须重置,防止后续文章循环出现异常。
内容的提问来源于stack exchange,提问作者Arun Gopal
相关产品推荐
相关产品推荐

