使用ID显示自定义帖子

时间:2016-02-06 18:07:11

标签: wordpress

在我的主题中,我有一个名为“滑块”的自定义帖子类型。在这里,用户可以在meta-box上传图像。因此,图像在wp_post_meta表中保存为元数据。

现在我要做的是,使用其ID显示滑块。

我确实喜欢以下但没有结果

$my_query = new WP_Query('post_type=sliders&p=411');

if( $my_query->have_posts() ) {
   while ($my_query->have_posts()) : $my_query->the_post(); 
   the_post();
 endwhile;
}

这不会显示任何内容。甚至没有错误。无论如何,如果我使用the_title()而不是the_post(),它会显示滑块的标题。 the_author()相同它显示作者没有错误。

为什么这很奇怪?

1 个答案:

答案 0 :(得分:0)

根据 Codex the_post();

  

the_post();迭代The Loop中的post索引。检索下一个   发布,设置帖子,设置循环中的'财产到真。   此功能不会返回任何值。

我们如何使用此功能的示例:

<?php
if ( have_posts() ) {
    while ( have_posts() ) {

        the_post();

        he_title();

        the_content();

    }
}
?>

你没有解释好你的问题。无论如何要显示你的metabox只需使用API​​函数

echo get_post_meta( $id, 'metabox_name', true );
相关问题