如何获取特定页面的内容?

时间:2013-01-13 04:18:37

标签: wordpress

我想要示例获取页面的内容,其中slug sasd是私有的我只希望它出现在侧边栏中。

我试过这个(页面):

<?php
$the_slug = 'sasd';
$args=array(
  'name' => $the_slug,
  'post_type' => 'page',
  'post_status' => 'private',
  'numberposts' => 1
);
$my_posts = get_posts($args);
if( $my_posts ) {
echo 'ID on the first post found '.$my_posts[0]->ID;
}
?>

2 个答案:

答案 0 :(得分:1)

我不确定为什么你的代码不起作用......但它应该......

这是我的测试结果..

Array
(
    [0] => WP_Post Object
        (
            [ID] => 31
            [post_author] => 1
            [post_date] => 2013-01-13 04:23:39
            [post_date_gmt] => 2013-01-13 04:23:39
            [post_content] => fdsfsdfsdfsdfsdfsd
            [post_title] => sasd
            [post_excerpt] => 
            [post_status] => private
            [comment_status] => open
            [ping_status] => open
            [post_password] => 
            [post_name] => sasd
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2013-01-13 04:23:39
            [post_modified_gmt] => 2013-01-13 04:23:39
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://localhost/wordpress/?p=31
            [menu_order] => 0
            [post_type] => post
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

)
ID on the first post found 31

请确保您有sasd作为帖子的标题。它有一个永久链接,如http://localhost/wordpress/2013/01/13/sasd/

要显示内容,您需要这样做:$my_posts[0]->post_content

或者只是看看我发布的WP_Post Object,看看如何获​​取其他数据的模式..

答案 1 :(得分:0)

当你收到你的帖子时:

foreach( $my_posts as $post ) :
     setup_postdata($post); 
     the_content(); // displays the content of the post
endforeach;  

the_content的参考:http://codex.wordpress.org/Function_Reference/the_content