sasi kanth
sasi kanth

Reputation: 2937

How to get title from WP_Post Object

i was trying to see the list of child pages name with some description to display ..i use below code

$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;

i am getting array before using 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
      )

after foreach if i print $pagedet iam getting

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

i tried to call $pagedet['post_title'] but id does't display any thing ...thanks in advance

Upvotes: 13

Views: 45142

Answers (4)

Kıvılcım
Kıvılcım

Reputation: 419

Just to be sure, you should use every page data as column name.

For instance,

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

Upvotes: 14

Jacob Robertson
Jacob Robertson

Reputation: 33

Replace above foreach:

Take the array of objects ([0],[1],[2]...) and set (each) as singular instance of $pagedet.

foreach($portfolio_children as $pagedet) {

Now create variable $post_title to equal 'post_title' value of each object in array.

$post_title = $pagedet->post_title;

This would technically work, but you want to loop through each instance programmatically without identifying each object in the array.

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

Now you can write out each post_title value:

echo $post_title;
};

In summary

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

Upvotes: 0

Amir Iqbal
Amir Iqbal

Reputation: 881

Try this one. Giving just idea.

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

Upvotes: 5

Ben Racicot
Ben Racicot

Reputation: 5971

This is from my notes that I got working with your exact situation. Hope it helps.

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

Upvotes: 3

Related Questions