0

I have a custom query to display content of a certain template:

<?php
    $i = 0;
    $pages = get_pages(array(
        'meta_key' => '_wp_page_template',
        'meta_value' => 'sale.php'
    ));
    foreach($pages as $page){
        echo '<div class="content_'. $i .'">' . apply_filters( 'the_content', $page->post_content ) . '</div>';
        echo '<div class="title_' . $i . '">' . apply_filters( 'the_title', $page->post_title) . '</div>';
        echo '<p>' . the_field('sale_images_url') . '</p>';
        $i++;
    }
?>

which works fine for the_content() and the_title() so far. Want I also wanna do is, to display the value of an Advanced Custom Field named sale_images_url.

But it displays nothing. Do I have to handle this different?

Thanks

closed as off-topic by Milo, birgire, fuxia Jan 2 '15 at 4:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1

In general, you need to pass the page ID to the_field and for that matter get_post_meta as well

You can try the following

the_field( 'sale_images_url', $page->ID );
  • the_field won't work within an echo statement, and get_post_meta isn't safe for outputting ACF fields because different fields have different formatting options which change their output. – Milo Jan 1 '15 at 19:13
  • @Milo OK, did not know about the get_post_meta part. On the_field, my answer is just an example of how to pass the page ID. And I did not include echo as anything without an get_ prefix are usually (but not always) echo'd and therefor no need to echo it manually – Pieter Goosen Jan 1 '15 at 19:21
  • in the context of OP's code, the_field will produce incorrect results. – Milo Jan 1 '15 at 19:25
  • @PieterGoosen thanks again for your help! Your first snippet worked! – user49869 Jan 1 '15 at 19:34
  • Glad it helped. Enjoy :-) – Pieter Goosen Jan 1 '15 at 19:35
0

You can print custom filed value with get_field function in ACF you can do this.

Inside the loop.

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

Outside the loop.

<?php echo get_field( 'sale_images_url', $page->ID ); ?>

Removed get_post_meta example because as pointed out by @Milo it will not produce correct result. Thanks.

  • but this is the Advanced Custom Fields plugin. Which uses the_field() as its own function. Please see the link in OP. – user49869 Jan 1 '15 at 18:32
  • Okay I have updated the answer. But get_post_meta will also work. – Robert hue Jan 1 '15 at 18:35
  • you can't always use get_post_meta as a replacement for get_field, because different ACF fields have different formatting options which change their output. – Milo Jan 1 '15 at 19:12
  • I think it's same because output for both can be string or array for different ACF fields. And in both cases, we will need to use same method to output required data. Isn't it? – Robert hue Jan 1 '15 at 19:19
  • an example- the taxonomy field stores a term's ID, but it might output either the ID or a term object, depending on the return value setting for that field, so in some cases the two functions will produce different output. – Milo Jan 1 '15 at 19:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.