你可以攻击get_pages();
函数的另一种方法可以更好地对代码进行排序并更好地阅读,请看this link 了解更多信息。
<?php $args = array(
\'sort_order\' => \'ASC\',
\'sort_column\' => \'post_title\', // sorting by alphabetical order of post title for example
\'hierarchical\' => 1,
\'exclude\' => \'\',
\'include\' => \'\', // edit this parameter instead of $pages = get_pages(\'include=2,4,5,8\');
\'meta_key\' => \'\',
\'meta_value\' => \'\',
\'authors\' => \'\',
\'child_of\' => 0,
\'parent\' => -1,
\'exclude_tree\' => \'\',
\'number\' => \'\',
\'offset\' => 0,
\'post_type\' => \'page\',
\'post_status\' => \'publish\'
); //edit whatever parameter you need to get what you need (sort_column to select which column to sort by), this list shows all parameters!!!
$pages = get_pages($args);
foreach ($pages as $page_data) {
$content = apply_filters(\'the_content\', $page_data->post_content);
$title = $page_data->post_title;
$slug = $page_data->post_name;
if( $page_data->ID == 2 ){ //format 1
get_template_part( \'content-1\', get_post_format() ); //get post template from content-1.php
}
elseif( $page_data->ID == 4 ){ //format 2
get_template_part( \'content-2\', get_post_format() ); //get post template from content-2.php
}
}
?>
我对
if
语句如果显示每个部分的代码太大,您可以简单地将其分解为其他文件以轻松地控制它并使其更具可读性。有关的更多用法
get_template_part();
功能参考
this link.