How to get the title from a WP_Post object

I tried to see a list of child page names 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 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 get an 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 calling $ pagedet ['post_title'], but id doesn't display anything ... thanks in advance

+7
php foreach wordpress
source share
4 answers

To be sure, you should use each page as a column name.

For example,

 $page_data->post_content //is true, $page_data->the_title // is false. 
+7
source share

This is from my notes that I worked with your exact situation. Hope this 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>'; } ?> 
+3
source share

Try it. Give only an idea.

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

Replace above foreach:

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

 foreach($portfolio_children as $pagedet) { 

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

 $post_title = $pagedet->post_title; 

This will technically work, but you want to program 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 each post_title value:

 echo $post_title; }; 

Finally

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

All Articles