2017-02-24 84 views
0

我有ACF和WordPress開發者的初學者。 所以,我想從WordPress面板中選擇要在div中顯示哪個標題帖子。我有一些名爲「hot_news」的ACF文件,它是post對象。返回的內容是發佈對象,而不是ID。從ACF獲取帖子標題 - 選擇帖子

我也有「顯示帖子是否等於(某個帖子標題)」。

這是我的代碼:

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

當我把它顯示出來,但不會顯示文章標題。哪裏不對?

+0

您在哪裏打印標題? –

+0

@MujeebuRahman這裏: ZAM666

回答

0

從ACF檢索的字段,你應該使用get_field所以

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

打印名爲 「hot_news」 目前的職位自定義字段。

如果你願意,你可以指定一個帖子ID:

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

我試過這個,沒有結果... – ZAM666

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)代碼可能是有意義的。

+0

呃,不行。你能否將它添加到我的代碼中,並向我展示這一點,以確保我能很好地應用這個? – ZAM666

+0

編輯我的答案 –

+0

謝謝,但它仍然無法正常工作... – ZAM666