Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to get page content

Tags:

wordpress

I have to get specific page content (like page(12))

I used that :

  <?php $id=47; $post = get_page($id); echo $post->post_content;  ?> 

Work nice execpt for compatibility with translations, it returns both French and English text

But the loop is fine, return only the good language version

<?php if(have_posts()) : while(have_posts()) : the_post(); ?> <div id="post"> <?php the_content(); ?> </div> <!-- .post --> 

So the question.... HOW to get a specific page content inside the loop...

like image 646
menardmam Avatar asked Mar 15 '11 20:03

menardmam


People also ask

How do I find page contents page ID in WordPress?

If you need to get the page ID you can easily do this by logging into your WordPress website admin and opening the page, post or custom post type for editing. The page id is visible in the address bar as shown in the screenshot below.

How do I get data from a page in WordPress?

get_page( int|WP_Post $page, string $output = OBJECT, string $filter = 'raw' ) Retrieves page data given a page ID or page object.


2 Answers

I've answered my own question. Call apply_filter and there you go.

<?php  $id=47;  $post = get_post($id);  $content = apply_filters('the_content', $post->post_content);  echo $content;   ?> 
like image 110
menardmam Avatar answered Oct 26 '22 05:10

menardmam


get page content by page name:

<?php $page = get_page_by_title( 'page-name' ); $content = apply_filters('the_content', $page->post_content);  echo $content; ?> 
like image 20
Alex Avatar answered Oct 26 '22 07:10

Alex