問題描述
我必須得到具體的頁面內容 (如頁面 (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 -->
所以問題…. 如何獲得一個特定的頁面內容,讓迴圈…
最佳解決方案
我回答了我自己的問題。呼叫 apply_filter,你去。
<?php
$id=47;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
次佳解決方案
透過頁面名稱獲取頁面內容:
<?php
$page = get_page_by_title( 'page-name' );
$content = apply_filters('the_content', $page->post_content);
echo $content;
?>
第三種解決方案
透過 id 獲取內容的簡單,快速的方法:
echo get_post_field('post_content', $id);
如果你想獲得格式化的內容:
echo apply_filters('the_content', get_post_field('post_content', $id));
與頁面,帖子和定製帖子。
第四種方案
我用這個:
<?php echo get_post_field('post_content', $post->ID); ?>
這更簡潔:
<?= get_post_field('post_content', $post->ID) ?>
第五種方案
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;
?>
第六種方案
只需複製並貼上此程式碼即可獲取您的頁面內容。
<?php
$pageid = get_the_id();
$content_post = get_post($pageid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。