wordpress 获取页面内容的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5317366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper way to get page content
提问by menardmam
I have to get specific page content (like page(12))
我必须获得特定的页面内容(如 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...
所以问题......如何在循环中获取特定页面内容......
回答by menardmam
I've answered my own question. Call apply_filter
and there you go.
我已经回答了我自己的问题。打电话apply_filter
就可以了。
<?php
$id=47;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
回答by Alex
get page content by page name:
按页面名称获取页面内容:
<?php
$page = get_page_by_title( 'page-name' );
$content = apply_filters('the_content', $page->post_content);
echo $content;
?>
回答by Fred K
I used this:
我用过这个:
<?php echo get_post_field('post_content', $post->ID); ?>
and this even more concise:
这更简洁:
<?= get_post_field('post_content', $post->ID) ?>
回答by Creaforge
A simple, fast way to get the content by id :
通过 id 获取内容的简单快速方法:
echo get_post_field('post_content', $id);
And if you want to get the content formatted :
如果您想格式化内容:
echo apply_filters('the_content', get_post_field('post_content', $id));
Works with pages, posts & custom posts.
适用于页面、帖子和自定义帖子。
回答by naveen kumar
Just only copy and paste this code it will get your page content.
只需复制并粘贴此代码即可获取您的页面内容。
<?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;
?>
回答by keithshaw
The wp_trim_words function can limit the characters too by changing the $num_words variable. For anyone who might find useful.
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;
?>
回答by Etienne Dupuis
One liner:
一个班轮:
<? if (have_posts()):while(have_posts()): the_post(); the_content(); endwhile; endif; ?>
回答by Dinesh Bhimani
$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 you want specific page of content then write in loop
//如果你想要特定的内容页面然后写在循环中
if(get_the_ID=='11')//make sure to use get_the_ID instead the_ID
{
echo get_the_ID();
the_title();
the_content();
}