Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 400
sasi kanth Avatar asked Aug 06 '13 10:08

sasi kanth


People also ask

How do I get the title of a WordPress post?

You can easily echo the get_the_title function and it'll output the current global $post object's title. This will work in most use cases and likely what you need. Alternatively you can set the post ID in the get_the_title function to get a specific post's title.

How do you get a title by id?

You can use the function get_the_title() to get the title of a page as a variable just by providing the ID. If you don't provide an ID, then the function will try to fetch the ID for the current page.

How do I find the post title and description in WordPress?

get_the_title( int|WP_Post $post ): string. Retrieves the post title.

What WordPress function do you use to display the title of the post?

the_title( string $before = '', string $after = '', bool $echo = true ): void|string. Displays or retrieves the current post title with optional markup.


2 Answers

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.
like image 189
Kıvılcım Avatar answered Sep 22 '22 16:09

Kıvılcım


Try this one. Giving just idea.

<?php 
    $post = get_post($_GET['id']); 
    $post->post_title;
 ?>
like image 43
Amir Iqbal Avatar answered Sep 18 '22 16:09

Amir Iqbal