获取页面内容的正确方法

men*_*mam 57 wordpress

我必须得到特定的页面内容(如第(12)页)

我用过:

  <?php $id=47; $post = get_page($id); echo $post->post_content;  ?>
Run Code Online (Sandbox Code Playgroud)

与qtranslate兼容的工作很好的execpt它返回法语和英语文本

但循环很好,只返回良好的语言版本

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

所以问题......如何获取特定的页面内容以确保循环...

men*_*mam 144

我已经回答了我自己的问题.打电话apply_filter,你去.

<?php 
$id=47; 
$post = get_post($id); 
$content = apply_filters('the_content', $post->post_content); 
echo $content;  
?>
Run Code Online (Sandbox Code Playgroud)

  • get_page已被弃用:http://codex.wordpress.org/Function_Reference/get_page.请改用get_post. (7认同)
  • 为什么需要apply_filters?只是$ post-> post_content似乎也一样,请问有什么区别吗? (2认同)
  • apply_filters 用于万一某个地方有一个插件想要“应用过滤器”到 `the_content` - 这有利于向前兼容性 (2认同)

Ale*_*lex 37

按页面名称获取页面内容:

<?php
$page = get_page_by_title( 'page-name' );
$content = apply_filters('the_content', $page->post_content); 
echo $content;
?>
Run Code Online (Sandbox Code Playgroud)


Fre*_*d K 9

我用过这个:

<?php echo get_post_field('post_content', $post->ID); ?>
Run Code Online (Sandbox Code Playgroud)

这更简洁:

<?= get_post_field('post_content', $post->ID) ?>
Run Code Online (Sandbox Code Playgroud)


Cre*_*rge 7

通过id获取内容的简单快捷方式:

echo get_post_field('post_content', $id);
Run Code Online (Sandbox Code Playgroud)

如果您想要格式化内容:

echo apply_filters('the_content', get_post_field('post_content', $id));
Run Code Online (Sandbox Code Playgroud)

适用于页面,帖子和自定义帖子.


nav*_*mar 5

仅复制并粘贴此代码即可获取您的页面内容。

<?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;
                ?>
Run Code Online (Sandbox Code Playgroud)