Support

Account

Home Forums Front-end Issues Syntax Issue: Display Custom Field Inside Loop?

Solved

Syntax Issue: Display Custom Field Inside Loop?

  • I used the 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 updated single.php page. I think my syntax is wrong for the second echo statement 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>
  • What does $recent["news_source"] return? I don’t think it returns anything useful because there is no “news_source” item in the post array. Try it with get_field('news_source') or possibly get_field('news_source',$recent['ID']).

  • Yep – changing it to get_field("news_source",$recent["ID"]) did the trick and now it’s returning the appropriate content. Thanks!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.