【问题标题】:Get post title from ACF - select post从 ACF 获取帖子标题 - 选择帖子
【发布时间】:2017-07-15 14:34:37
【问题描述】:

作为开发人员,我有 ACF 和 WordPress 的初学者。 因此,我想从 WordPress 面板中选择必须在 div 中显示的标题帖子。我有一些名为“hot_news”的 ACF 文件,它是 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>

当我让它显示时,但不显示帖子标题。怎么了?

【问题讨论】:

  • 你在哪里打印标题?
  • @MujeebuRahman 在这里:

标签: wordpress field advanced-custom-fields


【解决方案1】:

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

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

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

如果你愿意,你可以指定一个帖子 ID:

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

【讨论】:

    【解决方案2】:

    您在问题中说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) 代码可能是有意义的。

    【讨论】:

    • 呃,不行。您能否将其添加到我的代码中并显示给我看,以确保我很好地应用了它?
    • 你能提供更多关于什么不起作用的信息吗?有没有显示的链接?尝试运行 var_dump($hotnews) 并将输出粘贴到此处。
    • var_dump($hotnews) 给出“NULL”
    • 好的,那么 hot_news 字段没有被正确存储或访问。在 ACF 的管理屏幕中,hot_news 字段是附加到特定帖子还是在选项面板中?基本上,你在wp-admin区域中设置hot_news的值在哪里?
    猜你喜欢
    • 2010-12-04
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多