How do I get title and content on another page?

1

I wondered if you could get the title and content of a page you created on another page.

For example; I created a page called Home and I also created a template for that page, and I did the same for the page called quem somos .

In the home page I would like to show a brief description of the contents of the quem somos page.

Is this possible?

    
asked by anonymous 21.09.2016 / 13:14

1 answer

1

You can, in the home template, create a specific loop by searching the information for the who we are page. Something like that

$query = new WP_Query(array( 'pagename' => 'quem-somos' ));

if($query->have_posts()){
    while($query->have_posts()){
        $query->the_post();

        $titulo = the_title();
        $conteudo = the_content();

        # code....
    }
}

wp_reset_postdata(); // restaurando a global $post

must meet your need. Some points that deserve attention:

  • A page in WP is also a type of post . So the loop works. Because there is only one page with the name "who we are" , there will only be a return.
  • The WP_Query constructor parameter (you can see all the various parameters here ) is an associative array, and the pagename key is necessarily the slug of the page. See, the slug , not the name. You can read more about slugs here .
  • 21.09.2016 / 14:55