2

The thumbnail and title shows up as it should, but the post meta displays on the top of the page. What is the correct code for retrieve the post metadata?

function evasmat_posts_shortcode(){
    $q = new WP_Query(
        array( 'orderby' => 'date', 'posts_per_page' => '4')
    );

    $list = '<div class="cards">';

    while($q->have_posts()) : $q->the_post();

        $list .= '<div class="card">' .  get_the_post_thumbnail() . '<h2 class="entry-title-index"><a href="'.get_permalink().'">'.get_the_title() .'</a></h2>'  .'<div class="entry-meta">' . get_post_meta (evasmat_posted_on() ) .'</div></div>';


    endwhile;

    wp_reset_query();

    return $list . '</div>';

}

add_shortcode('recent-posts', 'evasmat_posts_shortcode');
  • evasmat_posted_on() is a function in your theme? Or is it custom post meta data? – input Mar 31 '17 at 13:00
  • It is a function in my theme. – Eva Mar 31 '17 at 17:00
  • please post the full code of that function. does the function return or echo the result? – Michael Mar 31 '17 at 18:23
1

Since evasmat_posted_on() is a function in your theme, you don't need to use get_post_meta to get the date. You can call the function directly so:

function evasmat_posts_shortcode(){
    $q = new WP_Query(
        array( 'orderby' => 'date', 'posts_per_page' => '4')
    );

    $list = '<div class="cards">';

    while($q->have_posts()) : $q->the_post();

        $list .= '<div class="card">' .  get_the_post_thumbnail() . '<h2 class="entry-title-index"><a href="'.get_permalink().'">'.get_the_title() .'</a></h2>'  .'<div class="entry-meta">' . evasmat_posted_on() .'</div></div>';


    endwhile;

    wp_reset_query();

    return $list . '</div>';

}

add_shortcode('recent-posts', 'evasmat_posts_shortcode');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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