Wordpress get_post_meta停止工作

时间:2013-06-24 18:39:30

标签: php wordpress function

我正在尝试使用get_post_meta从post meta返回一个值,它工作得很好然后突然停止工作。我已经尝试并检查了所有内容,我似乎无法让它再次运行 - 我正在使用的代码如下 - 无论我做什么,我都无法再返回元字段的元值。

代码被添加到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;
}

有什么想法吗?肉类名称“标题1”和“标题2”仍然存在。完全没有想法。

1 个答案:

答案 0 :(得分:0)

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

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