Retrieving ACF Post Object Data

Hi,

I’ve tried looking at the documentation and not sure if I’m heading towards the right direction or what’s wrong with it.

Basically all I want is beneath the at to display the location that’s queried from ACF

Initially I used PageLink, but I found out that it only returns the permalink and not the title.

I’ve set up the following code in my functions.php and while it does work, I’m not sure how to make it display in Pro, something like <a href="{{dc:acf:location_url}}">{{dc:acf:location_title}}</a>

This is the code in the functions.php

function display_acf_post_object_location() {
    // Check if we are on the specific page
    if ( is_page('apex-challenge-2') ) {
        // Assuming 'location' is your ACF Post Object field name
        $post_object = get_field('location');

        if ( $post_object ) {
            // Retrieve the post title
            $post_title = esc_html( $post_object->post_title );

            // Retrieve the post URL
            $post_url = get_permalink( $post_object->ID );

            // Display the title and URL
            echo 'Location Name: ' . $post_title;
            echo '<br>';
            echo 'Location URL: <a href="' . esc_url( $post_url ) . '">' . $post_title . '</a>';
        }
    }
}

function custom_acf_content( $content ) {
    if ( is_page('apex-challenge-2') ) {
        ob_start();
        display_acf_post_object_location();
        $location_content = ob_get_clean();
        return $location_content . $content;
    }
    return $content;
}
add_filter( 'the_content', 'custom_acf_content' );

Hello Omar,

Thanks for writing in! You will have to run a Looper to be able to display those locations. Please check out this video tutorial instead:

Best Regards.

Hmm, I’m only displaying 1 location though, so don’t think I’ll need a looper this time around? And the ACF is set to only allowing selecting 1 location.

Hello Omar,

If your ACF post field, location_title returns a Text content, you can use {{dc:acf:post_field field="location_title"}} to display the field value. If ever the field is a Post Object, you would run a looper to be able to display the data of that post object.

We would love to check your ACF field and its return output and be able to give you a more detailed solution for your issue.

Hope this makes sense.

Thanks! That seemed to work!

Hey Omar,

You’re most welcome!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.