如何解决 WordPress 短代码函数中的致命错误:无法重新声明函数...

问题描述 投票:0回答:1

`我需要显示嵌套的子分类法并列出其帖子。 这就是我想要得到的 它可以工作,但我需要使用该函数作为短代码。将包含此函数的页面保存为简码时出现“致命错误:无法重新声明函数...”。

这是保存页面时的完整错误:

“致命错误:无法在 /nas/content/live 中重新声明 get_child_categories() (之前在 /nas/content/live/resonate2022/wp-content/themes/ResonateHealth-Theme/content/masterguide-categorylist.php:3 中声明) /resonate2022/wp-content/themes/ResonateHealth-Theme/content/masterguide-categorylist.php 第 3 行”

这是我的函数/文件“masterguide-categories.php”

<?php
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;
}

function get_posts_for_lastchild($category_id ){
    $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;
    return $html;
}

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

然后在“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');
wordpress fatal-error custom-taxonomy wordpress-shortcode redeclare
1个回答
0
投票

在模板中包含函数并不是最佳实践,因此您应该重构代码。

但是,有一个快速解决您的问题的方法,只有在尚未声明的情况下才可以声明函数,请执行以下操作:

<?php
if (!function_exists('get_child_categories')) {
    function get_child_categories( $parent_category_id ){
        /// your code here
    }
}
if (!function_exists('get_posts_for_lastchild')) {
    function get_posts_for_lastchild( $category_id ){
        /// your code here
    }
}
if (!function_exists('list_categories')) {
    function list_categories(){
        /// your code here
    }
}
echo list_categories();

这样你就可以解决当前的问题,但请记住函数永远不应该在模板中。

© www.soinside.com 2019 - 2024. All rights reserved.