0
votes

I'm using this piece of code in my Wordpress template:

<?php
    $args = array( 'numberposts' => '12', 'post_type' => 'training', 'post_status' => 'publish' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ) {
        echo '<div class="col-xs-12 col-md-4"><article><div class="kartel"><a href="' . get_permalink($recent["ID"]) . '">';
        if ( has_post_thumbnail( $recent["ID"]) ) {
              echo  get_the_post_thumbnail($recent["ID"],'medium');
        }
        echo '</a></div><a href="' . get_permalink($recent["ID"]) . '"><h3>' .   $recent["post_title"].'</h3></a> ';

        echo '<em>Doelgroep //</em>
        <p>One-liner/super korte omschrijving</p>';
        echo '<a href="' . get_permalink($recent["ID"]) . '">Tell me more</a> ';
        echo '</article></div>';
    }
    wp_reset_query();
?>

Thing is that I now want to add a custom field (let's say 'custom_field') that displays a custom excerpt underneath the thumbnail for the grid. I can get the usual fields (excerpt, title, etc.) but not the custom fields. For example the_field ('custom_field'); isn't working..

Any ideas/suggestions?

Hope to hear from you guys!

Filt

1
You aren't looping it with a wp query so you have to tell the get_field() or the_field() functions which page id you want to display from. try the_field( 'custom_field', $recent["ID"] );Wilco
Awesome, this works and perhaps not the best way to do it but at least the quickest! ThanksCodigit

1 Answers

1
votes

First of all change your approach to queries and Wordpress loops using the WP_Query class.

<?php
$args = array( 'numberposts' => '12', 'post_type' => 'training', 'post_status' => 'publish' );
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
    while( $loop->have_posts() ){
        $loop->the_post(); ?>
        <div class="col-xs-12 col-md-4">
            <article>
                <div class="kartel">
                    <a href="<?php the_permalink(); ?>">
                        <?php if( has_post_thumbnail("medium") ) {
                            the_post_thumbnail( "medium" );
                        }
                        ?>
                    </a>
                </div>
                <a href="<?php the_permalink(); ?>">
                    <?php the_title("<h3>","</h3>"); ?>
                </a>
                <em>Doelgroep //</em>
                <a href="<?php the_permalink(); ?>">Tell me more</a>
            </article>
        </div>
    <?php }
}
wp_reset_postdata();
?>

Later, in the loop of your posts, you can recall the custom field using:

the_field ('custom'); //which prints the result on screen

$var = get_field ('custom'); //or echo get_field ('custom') which returns the content in a variable.

If you want to recall a specific custom field inserted in a post or page or custom post type, you must use the following syntax:

the_field ('custom', $ post_id);

get_field ('custom', $ post_id)

That's all :)