从ACF获得帖子标题 - 选择帖子

时间:2017-02-24 16:51:17

标签: wordpress field advanced-custom-fields

我是初学者,ACF和WordPress是开发人员。 所以,我想从WordPress面板中选择要在div中显示哪个标题帖子。我有一个名为“hot_news”的ACF文件,它是post对象。返回的内容是post对象,而不是ID。

我还有“显示帖子是否等于(某些帖子标题)”。

这是我的代码:

<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>

当我显示它时,但不显示帖子标题。有什么问题?

2 个答案:

答案 0 :(得分:0)

要从ACF中检索字段,您应该使用 get_field 以便

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

打印名为“hot_news”的当前帖子自定义字段。

如果需要,您可以指定帖子ID:

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

答案 1 :(得分:0)

你在问题​​中说hot_news是一个帖子对象。因此,如果您尝试回显对象,则无法获得所需内容。

相反,你必须做类似的事情:

<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>

您可以在此处获取有关如何使用ACF帖子对象的更多信息:https://www.advancedcustomfields.com/resources/post-object/

如果您只需要简单的帖子标题,我的方法应该有用,但如果你开始需要永久链接到帖子和类似的东西,那么使用ACF文档使用的setup_postdata($post)代码可能是有意义的。 / p>

相关问题