2011-03-15 62 views
41

我一定要得到特定頁面的內容(如第(12))正確的方式來獲得頁面內容

我用的是:

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

工作不錯execpt與qtranslate兼容性使其返回法語和英語文字

但循環是好的,只返回了良好的語言版本

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

所以的問題....如何獲得一個特定的頁面內容insite循環...

回答

120

我已經回答了我自己的問題。致電apply_filter,你去了。

<?php 
$id=47; 
$post = get_post($id); 
$content = apply_filters('the_content', $post->post_content); 
echo $content; 
?> 
+0

快速和優雅。 – Mina 2012-04-23 22:33:42

+5

get_page已被棄用:http://codex.wordpress.org/Function_Reference/get_page。改用get_post。 – manishie 2013-10-08 16:24:40

+0

+1謝謝!當在循環中使用wp_mail時,這很方便。 'get_the_content();'沒有把格式。這給了我很大的時間! – Davis 2014-03-26 17:07:20

30

獲取頁面內容的頁面名稱:

<?php 
$page = get_page_by_title('page-name'); 
$content = apply_filters('the_content', $page->post_content); 
echo $content; 
?> 
+0

也''get_page_by_path'可能會有用。 – Guss 2016-03-27 18:08:36

5

一個簡單,快捷的方式獲得通過ID內容:

echo get_post_field('post_content', $id); 

如果你想獲得格式化的內容:

echo apply_filters('the_content', get_post_field('post_content', $id)); 

與頁面,post s &自定義帖子。

1
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$args = array('prev_text' >' Previous','post_type' => 'page', 'posts_per_page' => 5, 'paged' => $paged); 
$wp_query = new WP_Query($args); 
while (have_posts()) : the_post(); 
//get all pages 
the_ID(); 
the_title(); 

//if you want specific page of content then write 
if(get_the_ID=='11')//make sure to use get_the_ID instead the_ID 
{ 
echo get_the_ID(); 
the_title(); 
the_content(); 
} 

endwhile; 

//如果你想要的特定內容頁,然後在循環

if(get_the_ID=='11')//make sure to use get_the_ID instead the_ID 
    { 
    echo get_the_ID(); 
    the_title(); 
    the_content(); 
    } 
+0

這不回答這個問題; 'get_the_ID'獲取當前帖子的ID - 問題是在循環內尋找一個* specific *頁面。 – vicvicvic 2015-02-23 11:03:48

+0

如果你可以請編輯你的答案,並解釋你所展示的代碼是做什麼的,爲什麼/如何代碼回答這個問題,它可以真正幫助。 自己的代碼塊通常不是有用的答案。 – 2015-02-24 06:54:47

5

我用這個寫:

<?php echo get_post_field('post_content', $post->ID); ?> 

,這更簡潔:

<?= get_post_field('post_content', $post->ID) ?> 
3

wp_trim_words函數也可以限制字符通過改變$ num_words變量。對於任何可能有用的人。

<?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; 
?> 
4

只需複製並粘貼此代碼即可獲取您的網頁內容。

<?php 
       $pageid = get_the_id(); 
       $content_post = get_post($pageid); 
       $content = $content_post->post_content; 
       $content = apply_filters('the_content', $content); 
       $content = str_replace(']]>', ']]&gt;', $content); 
       echo $content; 
       ?> 
1

一個內膽:

<? if (have_posts()):while(have_posts()): the_post(); the_content(); endwhile; endif; ?> 
相關問題