【问题标题】:Wordpress display all pages on one pageWordpress 在一页上显示所有页面
【发布时间】:2011-08-11 09:00:24
【问题描述】:
我需要在一页上显示所有页面。
目前我正在使用它来引入每个页面:
<?php
$page_id = 5; //id example
$page_data = get_page( $page_id );
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
echo $content;
?>
但是如果创建了一个新页面,它将不会显示它,因为没有此代码及其 ID..
有没有办法让所有页面自动进入?
任何帮助将不胜感激,谢谢
【问题讨论】:
标签:
php
wordpress-theming
wordpress
【解决方案1】:
您可以使用get_pages(); 获取您博客的所有页面,像这样循环执行此操作:
$pages = get_pages();
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
echo $content;
}
【解决方案2】:
接受的答案返回的数组不包含“海关后”(在我的情况下我需要)。而且您可能需要检索具有特定属性的页面。使用args 获得更多自定义结果:
<?php $args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => 0,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
?>
并像这样使用它们:
<?php
foreach ($pages as $page) {
$title = $page->post_title;
echo $title. "<br />";
} ?>
Read more here
【解决方案3】:
另外,如果您使用的是二十个主题(我不知道它在其他人中如何工作),您可以轻松地格式化所有页面:
echo "<h1 class=\"entry-title\">".$title."</h1>"
."<div class=\"entry-content\">".$content."</div>";
【解决方案4】:
那么你也可以通过这段代码获取每个页面的内容。
$content = apply_filters( 'the_content', $content );
如果您为不同的页面使用自定义模板,仍然存在很大的问题,您可以获得内容,但不能在您的一个页面上获取模板文件