Get template name (slug) in function.php

Question

I have post filtering by taxonomy, everything works is good, but i want return another html dependence on page template. If this is page “about us” return another html tag. This is my working code, but i’cant get page slug or name from functions.php all functions isn’t work. How can I write the condition correctly ?

function get_projects() {
    global $post;
    $service_id = $_POST['service_id'] ? $_POST['service_id'] : ''; 
    $country_id = $_POST['country_id'] ? $_POST['country_id'] : '';
    $paged = $_POST['paged'] ? $_POST['paged'] : 1; 
    $return_html = ''; 
    $args = array(
        'post_type' => 'post',
        'paged' => $paged,
    );
    if (!empty($country_id) && !empty($service_id)) {
        $args['relation'] = 'AND';
    }
    if (!empty($service_id)) {
        $args['tax_query'][] = array(
            'taxonomy' => 'general',
            'terms'    => $service_id
        );
    }
    if (!empty($country_id)) {
        $args['tax_query'][] = array(
            'taxonomy' => 'projects_country',
            'terms'    => $country_id
        );
    }
    $projects = new WP_Query($args);
    $max_pages = $projects->max_num_pages;
    if ($projects->have_posts()):
        $return_html .= '<div class="filter-item-block d-flex">';
        while ($projects->have_posts()): $projects->the_post();

            $thumbnail_img = get_the_post_thumbnail_url( $post->ID, 'filter-thumbnail' );
            $title = get_the_title();
            $permalink = get_the_permalink();
            $redirect_link = get_field('redirect_link');
            $date = get_the_date();
            $view = gt_get_post_view();

            $return_html .= '<div class="f-item d-flex">';
            $return_html .= '<div class="f-item-img">';
            $return_html .= '<img src=" '. $thumbnail_img .'?>" alt="">';
            $return_html .= '<div class="item-overlay">';
            $return_html .= '<div class="item-view d-flex">';
            $return_html .= '<div class="date">';
            $return_html .= $date;
            $return_html .= '</div>';
            $return_html .= '<div class="view-post d-flex">';
            $return_html .=  '<a href="'. get_author_posts_url($post->post_author) .'" class="filter-author-link" > '. get_the_author() .' </a>';
            $return_html .= '</div>';
            $return_html .= '</div>';
            $return_html .= '</div>';
            $return_html .= '</div>';

            $return_html .= '<div class="f-item-bottom">';
            $return_html .= '<div class="bottom-row d-flex">';
            $return_html .= '<div class="item-name">';
            $return_html .= '<a href="' . $permalink . '" class="item-link">';
            $return_html .= $title;
            $return_html .= '</a>';
            $return_html .= '</div>';
            $return_html .= '<div class="bookmark"></div>';

            $return_html .= '</div>';
            $return_html .= '</div>';

            $return_html .= '</div>';
        endwhile;
        $return_html .= '</div>';
    endif;
    if ($paged < $max_pages)  { 
        $next_page = $paged + 1; 
        $return_html .= '<div id="load-more-events" class="blue-btn load-more" data-page="'. $next_page .'" data-service-id="'. $service_id .'" data-country-id="'.$country_id.'">Show more</div>';
    }
    wp_reset_postdata();
    echo $return_html;
    wp_die(); 
}


add_action('wp_ajax_get_projects', 'get_projects');
add_action('wp_ajax_nopriv_get_projects', 'get_projects');
0
Yavar Mammadov 4 years 2019-11-05T08:21:13-05:00 0 Answers 82 views 0

Leave an answer

Browse
Browse