Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom function won't work in JS but human_time_diff() will WordPress

  • SOLVED

I have a function the_rooms() defined in functions.php 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 (located at https://gist.github.com/anonymous/adfedb7495f3a6340781 starting at line 128), it returns nothing. However, works and returns the correct time for each post.

To include the map-single-javascript.php file, I'm using {include 'snippets/map-single-javascript.php'} as found in gist.github.com/anonymous/db870015a2a353b50de4 on line 278.

This is using the WpLatte engine found at: http://www.ait-themes.com/documentation/wplatte-templating-engine/.

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

What am I doing wrong?

Answers (1)

2014-07-20

Hariprasad Vijayan answers:

Hello,

You are trying to include map-single-javascript.php in header.php, the_rooms() function is used for displaying the terms of taxonomy "ait-dir-item-rooms". And it can only accessible in single.php.

In the code,

$rooms = wp_get_post_terms($latteParams['post']->id, 'ait-dir-item-rooms');

$latteParams['post']->id might be null in header.php so its not returning any value, and its not null in single.php so it returns the terms.

Does that make sense?

Regards,
Hariprasad


GoForJacob comments:

That's sounds right! Let me try it real quick and I'll get back to you.


GoForJacob comments:

I keep getting error messages, but I think that you're on to something. Here's the single.php file that it works in: https://gist.github.com/anonymous/c4ec57e54c4cb6ba5b42.

I'll keep trying things though.


Hariprasad Vijayan comments:

Hi,

Could you show your single.php? The last provided git url isn't single.php.


GoForJacob comments:

I should have clarified. That's the custom single for that post type. It's where the_rooms() works in. My apologies.


GoForJacob comments:

It turns out that the file I've been needing to edit is directory.php (found at: [[LINK href="https://gist.github.com/anonymous/8efb072c434caacb3a89#file-directory-php-L360"]]https://gist.github.com/anonymous/8efb072c434caacb3a89#file-directory-php-L360[[/LINK]]). The code I have is on line 360.

In my map-javascript.php file, I'm calling the rooms with {!$item->rooms} as seen below:


data: '<div class="marker-holder"><div class="marker-content{ifset $item->thumbnailDir} with-image"><a href="{!$item->link}"><img src="{thumbnailResize $item->thumbnailDir, w => 280, h => 160}" alt="">{else}">{/ifset}<div class="map-item-info"><div class="title">&#36;'+{ifset $item->optionsDir["price"]}{$item->optionsDir["price"]}+{/ifset}' <small>{!$item->rooms}bd/{!$item->baths}ba</small></div><div class="address">'+{ifset $item->optionsDir["address"]}{$item->optionsDir["address"]|nl2br}+{/ifset}',<br>'+{ifset $item->optionsDir["address2"]}{$item->optionsDir["address2"]|nl2br}+{/ifset}'</div><div class="timestamp">{!$item->time}</div></div></a><div class="arrow"></div><div class="close">x</div></div></div></div>'


It's now returning the appropriate category names, but it's not in the appropriate place. It just injects them somewhere in the body and where it's supposed to display it, it only shows "array" as in the picture I submitted.