0

I used the ACF (Advanced Custom Fields) plugin to create two custom fields (news_source and news_link) for a new post type (news). I'm trying to use the following code to display the title and the two new fields on my home page. It pulls the title but it doesn't pull the custom fields even though I can see the custom fields on the single.php page. I think my syntax is wrong for the second echo statment because it isn't returning a value between the paragraph tags. What did I do wrong?

<ul>
 <?php
    $recent_posts = wp_get_recent_posts(array('post_type'=>'news'));
    foreach( $recent_posts as $recent )
     {
       echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
       echo '<p>' . get_field($recent["news_source"]) . '</p>';
     }
 ?>
</ul>
0
2
    <ul>
 <?php
    $recent_posts = wp_get_recent_posts(array('post_type'=>'news'));
    foreach( $recent_posts as $recent )
     {
       echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
       echo '<p>' . get_field("news_source",$recent["ID"]) . '</p>';
     }
 ?>
</ul>

It looks your are passing get field parameters incorrectly. I assumed "news_source" is your custom field name in acf.

1
  • Ha - yep! Thank you that did the trick.
    – Asad K.
    Aug 25 '21 at 15:50

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