仅查询循环内自定义帖子类型的父页面

时间:2011-12-01 15:09:28

标签: wordpress

我在下面的代码中查询了帖子和帖子类型:

 <?php
              $args = array('post_type' => 'apartmentlisting', 'parent' => 0, 'showposts'=>'-1');
              query_posts($args);
          ?>
          <?php if (have_posts()) : ?>
          <?php while (have_posts()) : the_post(); ?>

我正在尝试查询并仅返回我有10个父页面的父页面,每个页面都有大约4-5个子页面。有没有办法让父母回归?

我一直在WP和谷歌上挖掘代码,什么都没有。我只找到了有关页面ID为XX的父级的回帖的文章。

有什么想法吗?

2 个答案:

答案 0 :(得分:11)

如果没有父级,则父级为零。所以你的查询应该有效。但参数是'post_parent'而不是'parent'。并且'showposts'已弃用,请改用“posts_per_page”。所以试试这个:

$args = array('post_type' => 'apartmentlisting', 'post_parent' => 0, 'posts_per_page'=>'-1');

答案 1 :(得分:0)

您是否考虑过使用以下内容:

<?php get_post_ancestors( $post ) ?>

其中$ post是帖子ID?如果您在the_content的动作挂钩中,则可以使用以下内容获取帖子的ID:

global $wp_query;
$page = $wp_query->page->ID;