Proper way to get page content

2019-03-07 18:00发布

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 qtranslate it return 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 insite the loop...

标签: wordpress
8条回答
爷的心禁止访问
2楼-- · 2019-03-07 19:01

The wp_trim_words function can limit the characters too by changing the $num_words variable. For anyone who might find useful.

<?php 
$id=58; 
$post = get_post($id); 
$content = apply_filters('the_content', $post->post_content); 

$customExcerpt = wp_trim_words( $content, $num_words = 26, $more = '' );
echo $customExcerpt;  
?>
查看更多
一纸荒年 Trace。
3楼-- · 2019-03-07 19:03

get page content by page name:

<?php
$page = get_page_by_title( 'page-name' );
$content = apply_filters('the_content', $page->post_content); 
echo $content;
?>
查看更多
登录 后发表回答