How to get data in functions.php

  php, wordpress

I’m currently trying to get dk_get_case () in functions.php to work on other pages (such as slug2), which only works on slug1 page.
I have code in slug1.php that can extract the string "slug1". I put it in dk_get_case() in functions.php, but dk_get_case() itself stopped working.
Is it not possible to get it with functions.php?
Code
functions.php


add_action('wp_ajax_get_case', 'dk_get_case');
add_action('wp_ajax_nopriv_get_case', 'dk_get_case');

function dk_get_case() {
    $headers['Access-Control-Allow-Origin'] = '*';
    $return = ['status' => false, 'data' => [], 'message' => ''];
    //Error code
    $terms_1 = get_the_terms($post->ID,'case_category');
    $term_1 =$terms[0]->slug;

    $case_clinics = [19,20,24,32,22,12,29,44,14,47];

    foreach($case_clinics as $key => $case_clinic){

    $dk_posts = get_posts(
            array(
                'showposts' => -1,
                'post_type' => 'case',
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'case_clinic',
                        'field' => 'term_id',
                        'terms' => $case_clinic
                    ),
                                    array(
                        'taxonomy' => 'case_category',
                        'field' => 'slug',
                        //Works with "slug1"
                        'terms' => $term_1,
                    )
                )
            )
        );
        if(count($dk_posts) > 0){
            foreach($dk_posts as $dk_post){
                $attachment_id = CFS()->get('case_image', $dk_post->ID);
                                $case_name1 = CFS()->get('case_name1', $dk_post->ID);
                                $case_clinic = CFS()->get('case_clinic', $dk_post->ID);
                                $case_name = CFS()->get('case_name', $dk_post->ID);
                                $case_price = CFS()->get('case_price', $dk_post->ID);
                                $case_risk = CFS()->get('case_risk', $dk_post->ID);
                $return['data'][$key][] = [
                    'id' => $dk_post->ID,
                                        'case_clinic'=>$case_clinic,
                    'thumb' => wp_get_attachment_image($attachment_id, 'case_clinic'),
                    'popup' => wp_get_attachment_image($attachment_id, 'full'),
                                        'case_name1'=>$case_name1,
                                        'case_clinic'=>$case_clinic,
                                        'case_name'=>$case_name,
                                        'case_price'=>$case_price,
                                        'case_risk'=>$case_risk,
                ];
            }
        }else{
            $return['data'][$key][] = [];
        }
    }
    $return['status'] = true;
//        $last = count($return['data']) - 1;
//        $return['data'][$last][] = $return['data'][$last][0];
    // print_r($return['data']);die();
    echo json_encode($return);
    die;
}

taxonomy-case_category-slug1.php

//Result is "string: slug1"
    $terms_1 = get_the_terms($post->ID,'case_category');
    $term_1 =$terms[0]->slug;
var_dump($term_1);

Source: Ask PHP

LEAVE A COMMENT