wordpressfatal-errorcustom-taxonomywordpress-shortcoderedeclare

How can I solve Fatal error: Cannot redeclare function... in a wordpress shortcode function


`I need to display nested child taxonomies and list its posts. This is what I am trying to get Its working, but I need to use the fuction as a shortcode. Getting "Fatal error: Cannot redeclare function..." while saving the page with this function as a shortcode.

Here is the full error while saving the page:

"Fatal error: Cannot redeclare get_child_categories() (previously declared in /nas/content/live/resonate2022/wp-content/themes/ResonateHealth-Theme/content/masterguide-categorylist.php:3) in /nas/content/live/resonate2022/wp-content/themes/ResonateHealth-Theme/content/masterguide-categorylist.php on line 3"

Here is my function/file "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();
?>

Then in "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');

Solution

  • It is not the best practice to have functions in templates, so you should refactor your code.

    However, there is a quick solution to your problem, you can declare a function only if it is not already been declared, do this:

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

    This way you will solve your current problem, but remember that functions should never be in templates.