Wordpress 获取页面内容的正确方法
我必须获得特定的页面内容(如第(12)页) 我用的是:Wordpress 获取页面内容的正确方法,wordpress,Wordpress,我必须获得特定的页面内容(如第(12)页) 我用的是: <?php $id=47; $post = get_page($id); echo $post->post_content; ?> 为了与翻译兼容,它会返回法文和英文文本 但是循环很好,只返回好的语言版本 <?php if(have_posts()) : while(have_posts()) : the_post(); ?> <div id="post"> <?php the_c
<?php $id=47; $post = get_page($id); echo $post->post_content; ?>
为了与翻译兼容,它会返回法文和英文文本
但是循环很好,只返回好的语言版本
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div id="post">
<?php the_content(); ?>
</div> <!-- .post -->
所以问题是。。。。如何在循环中获取特定页面内容…我已经回答了自己的问题。调用
apply\u 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));
使用页面、帖子和自定义帖子
$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();
}
我用了这个:
<?php echo get_post_field('post_content', $post->ID); ?>
这更为简洁:
<?= get_post_field('post_content', $post->ID) ?>
wp\u trim\u words函数也可以通过更改$num\u 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;
?>
一个衬里:
<? if (have_posts()):while(have_posts()): the_post(); the_content(); endwhile; endif; ?>
get\u页面已被弃用:。使用get_post代替。+1谢谢!这在循环中包含wp_邮件时也很方便。”获取内容();'没有把格式放出来。这帮了我大忙$content=apply_filters('the_content',$post->post_content)//构建电子邮件的消息$message=$content;wp_邮件($to、$subject、$message、$headers);为什么需要应用过滤器?只是$post->post\u内容似乎是一样的,请问有什么区别吗?应用\u过滤器是为了防止某个地方有插件想“应用过滤器”到\u内容-这有利于转发兼容性这并不能回答问题<代码>获取当前帖子的ID
获取当前帖子的ID–问题是在循环中查找特定页面。如果您可以编辑您的答案,并解释您显示的代码的作用,以及该代码为什么/如何回答问题,这将非常有帮助。代码块本身通常不是有用的答案。另外,get\u page\u by\u path
也可能有用。这很好。就我的例子而言,我可以使用$content=get_post_字段('post_content',get_the_id())代码>