简体   繁体   中英

Wordpress get_post_meta stopped working

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.

You have spaces in you're meta-key (Title 1, Title 2). try saving 'em as title-1 and title-2 and fetch them like this:

get_post_meta(get_the_ID(), 'title-1', true);
get_post_meta(get_the_ID(), 'title-2', true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM