如何从WP_Post对象获取标题

时间:2013-08-06 10:14:11

标签: php wordpress foreach

我试图查看子页面列表名称以及一些描述以显示..i使用下面的代码

$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
// Get the page as an Object
$portfolio =  get_page_by_title('service');
// Filter through all pages and find Portfolio's children
$portfolio_children = get_page_children( $portfolio->ID, $all_wp_pages );
// echo what we get back from WP to the browser
echo "<pre>";print_r(
);

foreach($portfolio_children as $pagedet):

        echo $pagedet['post_title'];


 endforeach;

我在使用foreach之前得到数组

when i print  $portfolio_children iam getting out put like this 
 Array
(
 [0] => WP_Post Object
    (
        [ID] => 201  
         [post_title] => Website Hosting
     )
  [1]=> WP_Post Object

      (
              [ID] => 202  
         [post_title] => Website
      )

在foreach之后如果我打印$ pagedet iam得到

WP_Post Object
    (
        [ID] => 201  
       [post_title] => Website Hosting
     )

我试图调用$ pagedet ['post_title']但是id没有显示任何东西......提前感谢

4 个答案:

答案 0 :(得分:10)

为了确保,您应该将每个页面数据用作列名。

例如,

$page_data->post_content //is true,
$page_data->the_title // is false.

答案 1 :(得分:3)

这是我的笔记,我得到了你的确切情况。希望它有所帮助。

<?php 

    $my_wp_query = new WP_Query();
     $all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => -1));

    $childpg = get_page_children(8, $all_wp_pages);

    foreach($childpg as $children){
        $page = $children->ID;
        $page_data = get_page($page);
        $content = $page_data->post_content;
        $content = $page_data->the_title;
        $content = apply_filters('the_content',$content);
        $content = str_replace(']]>', ']]>', $content);
        echo '<div class="row-fluid"><span class="span4">'; 
        echo get_the_post_thumbnail( $page ); 
        echo '</span><span class="span8">'.$content.'</span></div>';
    } 
    ?>

答案 2 :(得分:1)

试试这个。给出正确的想法。

<?php 
    $post = get_post($_GET['id']); 
    $post->post_title;
 ?>

答案 3 :(得分:0)

替换上述foreach:

获取对象数组([0],[1],[2] ...)并将(每个)设置为$ pagedet的单个实例。

foreach($portfolio_children as $pagedet) {

现在创建变量$ post_title以等于&#39; post_title&#39;数组中每个对象的值。

$post_title = $pagedet->post_title;

这在技术上可行,但是您希望以编程方式遍历每个实例而不识别数组中的每个对象。

echo $pagedet[0]->post_title;
echo $pagedet[1]->post_title;
echo $pagedet[2]->post_title;

现在你可以写出每个post_title值:

echo $post_title;
};

总结

foreach($portfolio_children as $pagedet) {
    $post_title = $pagedet->post_title;
    echo $post_title;
};