ZAM666
ZAM666

Reputation: 31

Get post title from ACF - select post

I have beginner with ACF and WordPress as developer. So, I want to select which title post has to be displayed in div from WordPress panel. I have got some ACF file called "hot_news" which is post object. Returned content is post object, not ID.

I have got also "Show if post is equal to (some post title)".

This is my code:

<div class="bp1-col-1-1 news-line">
    <a class="button button-hot-news" href="#">Aktualności</a>
    <img class="hot-icon hot-icon-left" src="<?php echo get_bloginfo('template_url') ?>/images/warning-icon.png" alt="Hot news!">
    <div class="morquee-efect">
        <a class="hot-news-title" href="#"><?php the_field('hot_news'); ?></a>
    </div>
    <img class="hot-icon hot-icon-right" src="<?php echo get_bloginfo('template_url') ?>/images/warning-icon.png" alt="Hot news!">
</div>

When I make it is displayed, but does not display post title. What is wrong?

Upvotes: 0

Views: 3778

Answers (2)

Greg Burkett
Greg Burkett

Reputation: 1945

You said in your question that hot_news is a post object. Therefore, if you try to echo an object, you're not going to get what you want.

Instead, you'll have to do something like:

<div class="bp1-col-1-1 news-line">
    <a class="button button-hot-news" href="#">Aktualności</a>
    <img class="hot-icon hot-icon-left" src="<?php echo get_bloginfo('template_url') ?>/images/warning-icon.png" alt="Hot news!">
    <div class="morquee-efect">
        <a class="hot-news-title" href="#"><?php
          $hotnews = get_field('hot_news');
          if($hotnews) echo $hotnews->post_title;
        ?></a>
    </div>
    <img class="hot-icon hot-icon-right" src="<?php echo get_bloginfo('template_url') ?>/images/warning-icon.png" alt="Hot news!">
</div>

You can get more info on how to work with an ACF post object here: https://www.advancedcustomfields.com/resources/post-object/

My method should work if you just need the simple post title, but if you start needing permalinks to the post and stuff like that, it may make sense to use the setup_postdata($post) code that the ACF docs use.

Upvotes: 0

fabio
fabio

Reputation: 624

to retrieve a field from ACF you should use get_field so

  <?php echo get_field('hot_news'); ?>

prints the current post custom field named "hot_news".

If you want you can specify a post ID :

<?php echo get_field('hot_news'[,post_ID]); ?>

Upvotes: 0

Related Questions