0

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

我试过这个(页面):

<?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;
}
?>
4

2 回答 2

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我发布的,看看如何获​​取其他数据的模式..

于 2013-01-13T04:29:33.013 回答
0

当你收到你的帖子时:

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

参考the_contenthttp ://codex.wordpress.org/Function_Reference/the_content

于 2013-01-13T04:30:05.647 回答