<?php 
// Add Shortcode
function custom_post_service_shortcode() {
    // Initialize output variable
    $output = '';

    // Query custom post type 'service'
    $args = array(
        'post_type' => 'services',
        'posts_per_page' => -1, // -1 to retrieve all posts
    );
    $service_query = new WP_Query($args);

    // Check if there are posts
    if ($service_query->have_posts()) {
        // Start the loop
        while ($service_query->have_posts()) {
            $service_query->the_post();
            // Get the ACF field value for 'service_icon'
            $services_icon = get_field('services_icon', get_the_ID());

            // Get the title of the post
            $title = get_the_title();

            // Check if the current service item corresponds to the current page
            $current_class = '';
            if (is_singular('services') && get_the_ID() === get_queried_object_id()) {
                $current_class = 'act';
            }

            // Append service HTML to the output with the current class if applicable
            $output .= '<div class="service_left ' . esc_attr($current_class) . '">';

            $output .= '<a class="fulllink" href="' . esc_url(get_permalink()) . '"></a>';
            $output .= '<div class="service-icon"><img src="' . wp_kses_post($services_icon) . '" ></div>';
            $output .= '<h2>' . esc_html($title) . '</h2>';
            $output .= '</div>';
        }

        // Restore original post data
        wp_reset_postdata();
    } else {
        // If no posts found
        $output .= 'No services found.';
    }

    // Return the output
    return $output;
}
add_shortcode('custom_post_service', 'custom_post_service_shortcode');


?>