使用WordPress的AJAX中的PHP

I have a function the_rooms defined as:

function the_rooms() {
    $latteParams['post'] = WpLatte::createPostEntity(
        $GLOBALS['wp_query']->post,
        array(
            'meta' => $GLOBALS['pageOptions'],
        )
    );
    $rooms = wp_get_post_terms($latteParams['post']->id, 'ait-dir-item-rooms');
        if ( !empty( $rooms ) && !is_wp_error( $rooms ) ){
            foreach ( $rooms as $room ) {
                echo $room->name;
            }
    }
}

This works in my single.php, but when I call it in another file, map-single-javascript.php:

data:
    '<div class="marker-holder">
        <div class="marker-content with-image">
            <a href="{!$item->link}">
                <img src="{thumbnailResize $item->thumbnailDir, w => 120, h => 160}" alt="">
                <div class="map-item-info">
                    <div class="title">
                        &#36;'+{ifset $item->optionsDir["price"]}{$item->optionsDir["price"]}+{/ifset}' 
                        <small><?php the_rooms(); ?>bd/<?php the_baths(); ?>ba</small>
                    </div>
                    <div class="address">
                        '+{$item->optionsDir["address"]|nl2br}+'<br>'+{$item->optionsDir["address2"]|nl2br}+'
                    </div>
                    <div class="timestamp">
                        <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?>
                    </div>
                </div>
            </a>
            <div class="arrow"></div>
            <div class="close">x</div>
        </div>
    </div>'

It returns nothing. However, <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?> works and returns the correct time for each post.

I can't wrap my mind around why human_time_diff works but my the_rooms won't.

Is this because it isn't being called inside the loop? If so, neither is human_time_diff, but it works. How can I get it to work in map-single-javascript.php?

Thank you!