获取页面内容的正确方法

时间:2011-03-15 20:05:56

标签: wordpress

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

我用过:

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

为了与qtranslate的兼容性而努力工作,它返回法语和英语文本

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

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

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

8 个答案:

答案 0 :(得分:140)

我已经回答了我自己的问题。打电话给apply_filter然后你去吧。

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

答案 1 :(得分:37)

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

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

答案 2 :(得分:8)

我用过这个:

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

这更简洁:

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

答案 3 :(得分:7)

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

echo get_post_field('post_content', $id);

如果您想要格式化内容:

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

适用于网页,帖子和广告;自定义帖子。

答案 4 :(得分:5)

只需复制并粘贴此代码即可获取您的网页内容。

<?php
$title = $_POST['title'];
$body = $_POST['body'];
$author = $_POST['author'];
$post = "INSERT INTO post (title, body, author) VALUES ($title,$body,$author)";
$sql = mysqli_query($conn,$post);
?>

答案 5 :(得分:4)

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

答案 6 :(得分:2)

$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();
    }

答案 7 :(得分:2)

一个班轮:

<? if (have_posts()):while(have_posts()): the_post(); the_content(); endwhile; endif; ?>
相关问题