简体   繁体   中英

WordPress: use get_post_meta in function

I am building a website with the Listify theme and I want to add custom info to my map markers. ( http://fclibero.com/listings/ )

I can add stuff to the map markers by using this function:

function custom_listify_listing_data( $data ) {
    $data[ 'date_added' ] = get_post()->post_date;  
    return $data; 
} 
add_filter( 'listify_listing_data', 'custom_listify_listing_data' );

view raw

But the data I need is in the post_meta and it's called geolocation_lat and geolocation_long. I am assuming I need to use this function: https://developer.wordpress.org/reference/functions/get_post_meta/

How can I combine the two functions to get the lat and long from my database?

Thanks!

Looks like this can work for you:

function custom_listify_listing_data( $data ) {
    $postObject = get_post();
    $data[ 'date_added' ] = $postObject->post_date;
    // geolocation_lat and geolocation_long
    $data[ 'geolocation_lat' ] = get_post_meta($postObject->ID, 'geolocation_lat', true);
    $data[ 'geolocation_long' ] = get_post_meta($postObject->ID, 'geolocation_long', true);
    return $data;
}
add_filter( 'listify_listing_data', 'custom_listify_listing_data' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM