我有一個自定義帖子類型,其中包含使用高級自定義字段創建的自定義字段。我可以檢索所有這類的帖子,並用下面的代碼顯示圖像:檢索單個自定義字段的自定義帖子類型
<?php
$args = array('post_type' => 'image');
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); ?>
<?php $image = get_field('image_file'); ?>
<img src="<?php echo $image['url']; ?>" />
<?php endwhile; ?>
不過,我希望能夠檢索單個圖像的URL,而無需通過所有這些循環。我將如何去做這件事?
我已經試過這樣:
<?php
$args = array('post_type' => 'image', 'title' => 'Welcome to bird - image 1');
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); ?>
<?php $image = get_field('image_file'); ?>
<img src="<?php echo $image['url']; ?>" />
<?php endwhile; ?>
但它輸出的整個列表 - 我如何才能得到一個帖子?如果有幫助,對固定鏈接是這樣的:
/?image=welcome-to-bird-image-1
在第二個示例中,爲什麼不再使用WP_Query? –
啊,是的,我應該。我已經更新了它,但我仍然沒有收到單個帖子 - 它顯示了所有這些帖子。 – babbaggeii