wordpress - 如何在wordpress中只获取至少有一个孩子的父页面?
问题描述
我想显示作为父页面的页面列表,以及分配给它的至少一个孩子的页面列表。我正在使用以下查询,但不幸的是它没有给出预期的输出。我有什么遗漏吗,任何帮助都应该对我有用。
$args = array(
'post_type' => 'page',
'post_parent' => 0
);
// query
$the_query = new WP_Query( $args );
// loop through posts
if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post();?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; ?>
<?php endif;
解决方案
这对我来说是工作。试试这个代码
add_action( 'init', 'parent_pages' );
function parent_pages(){
$args = array(
'post_type' => 'page',
'numberposts' => -1
);
$the_query = get_posts( $args );
foreach ($the_query as $key) {
if(!empty(get_children( $key->ID ))){
echo '<a href="'.$key->guid.'">'.$key->post_title.'</a>';
}
}
}
推荐阅读
- java - 如何提高 apache poi 的内存使用和性能?
- python - 如何正确使用 mask_zero=True 进行预训练权重的 Keras 嵌入?
- angularjs - 警告:无效流:“FormatError:Flate 流中的错误 FCHECK:120、239”在 dropzone 上传后来自 springboot API 的 PDF 响应
- r - Facet_wrap 不与 geom_bar 一起使用,但与 geom_point 一起使用
- oracle - 错误:在 Oracle PLSQl 过程中比较两个表
- php - 如何在 php 中创建 Linnworks 应用程序(应用程序类型:渠道集成)?
- azure - Azure 使用 Rest API 创建资源监控 Webhook
- javascript - 尝试访问快速路由时未找到 404 错误
- jenkins - 通过 shell 脚本在 GCP VM 上部署 Jenkins
- javascript - 如何使用事件委托而不是将事件侦听器单独附加到每个项目?