Support

Account

Home Forums ACF PRO Display metadata of post object?

Solved

Display metadata of post object?

  • I’ve searched everywhere but cannot find the solution so far… hopefully someone in here knows.

    Using ACF Pro, I created a Post Object field that allows the user to select a single post. For the selected post, I need to display the featured image, post title, permalink, categories (categories don’t need to link) and publish date. I can only figure out how to display the thumbnail, title and permalink. Does anyone know how to output the categories and publish date from a Post Object?

    My code is below. I’ve made several attempts to display the category and all were failures, but here is one example. This throws the error “Notice: Array to string conversion… “. Which goes away if I take out all the category bits.

    I work more with css than php, so any help would be appreciated 🙂

    <?php
            $featured_post = get_field('primary_review');
            if( $featured_post ): 
              $permalink = get_permalink( $featured_post->ID );
              $title = get_the_title( $featured_post->ID );
              $categories = get_the_category($featured_post->ID);
            ?>
              <a href="<?php echo esc_url( $permalink ); ?>"><?php echo esc_html( $title ); ?></a>
                <?php the_post_thumbnail(); ?>
                <?php echo esc_html( $categories ); ?>
            <?php endif; ?>
  • You can get the published date with something like:

    $publishDate = get_the_time('F d, Y', $featured_post->ID);

    Change the format there, as needed. You’re getting the error on categories because of how you’re trying to spit out the $categories variable.

    $categories will be an array of categories so to display them, you’d want to loop through the array. I don’t know what you want from the category (a link to the category, just the name, etc.) but it’d be something like:

    foreach( $categories as $category ) {
        echo $category->name;
    }
  • Thank you so much @wpfieldwork, this worked like a charm!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.