0

プライベートなスラッグ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;
}
?>
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_content: http://codex.wordpress.org/Function_Reference/the_content

于 2013-01-13T04:30:05.647 に答える