我使用以下代码获取项目页面的所有子页面的列表,并将其显示在模板页脚元素的最近项目部分。
我注意到get_page_children()
方法返回有关该页的所有信息-post_author
, post_date
, post_content
, post_title
, 等等。是否有办法限制此操作,使其仅返回post_title
? 否则,在查询所有这些未在页脚中呈现的其他属性时会产生不必要的开销。
还有,有没有办法限制返回的对象数量?我只想按日期降序排列最近的四个。
<h3>Recent Projects</h3>
<ul class="footer-list">
<?php
// Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array(\'post_type\' => \'page\'));
// Get the page as an Object
$projects = get_page_by_title(\'Projects\');
// Filter through all pages and find Portfolio\'s children
$projects_children = get_page_children( $projects->ID, $all_wp_pages );
// echo what we get back from WP to the browser
foreach ($projects_children as $project):
?>
<li>
<a href="<?php echo get_permalink($project); ?>"><?php echo var_dump($project);//echo $project->post_title; ?></a>
</li>
<?php endforeach; ?>
</ul>
最合适的回答,由SO网友:tfrommen 整理而成
您只需返回ID,然后根据各自的ID获取每个标题。要将查询限制为仅四篇文章,您可以使用posts_per_page
.
我对您的上述代码进行了一些修改和优化,如下所示:
<h3>Recent Projects</h3>
<ul class="footer-list"><?php
$projects_page = get_page_by_title(\'Projects\');
$args = array(
\'post_type\' => \'page\',
\'post_parent\' => $projects_page->ID,
\'ignore_sticky_posts\' => true,
\'posts_per_page\' => 4,
\'fields\' => \'ids\',
);
$projects = new WP_Query($args);
foreach ($projects as $project) {
?><li>
<a href="<?php echo get_permalink($project); ?>"><?php echo get_the_title($project); ?></a>
</li><?php
}
?></ul>