0

I am trying to use get_post_meta to return a value from the post meta, it was working completely fine then suddenly stopped working. I've tried and checked everything and i cannot seem to get it working again - the code i'm using is as below - no matter what i do, i cannot return the meta value for the meta field any more.

The code is added to functions.php.

function latest()   {
$args = array( 'posts_per_page' => 1);                  
$last_5_posts_query = new WP_Query( $args );
while($last_5_posts_query->have_posts()) : 
    $last_5_posts_query->the_post();
    $link = get_permalink();
    $title = get_the_title();  
    $title1 = get_post_meta(get_the_ID(), 'Title 1', true);     
    $title2 = get_post_meta(get_the_ID(), 'Title 2', true);  
    $more = 'Read more...';    

    $content .= '<div class="top_titles">';
    $content .= '<h3><a href="#top_titles">'  .$title1.  '</a></h3>';
    $content .= '<h3><a href="#top_titles">'  .$title2.  '</a></h3>';
    $content .= '</div>';
endwhile;

return $content;
}

Any ideas? The meat names "Title 1" and "Title 2" still exist. Completely out of ideas.

4

1 回答 1

0

您的元键中有空格(标题 1,标题 2)。尝试将它们保存为 title-1 和 title-2 并像这样获取它们:

get_post_meta(get_the_ID(), 'title-1', true);
get_post_meta(get_the_ID(), 'title-2', true);
于 2013-06-24T21:25:19.387 回答