我有一个自定义的存档页面(带有地图和位置数据),用于自定义帖子类型。由于位置是分层次的(州、市、位置),我只想在归档页面上显示这些州,并在标记上显示孙儿的数量(使用google maps API)。带有文本的标记非常有效,因为它只抓取顶层位置。我现在的问题是得到一个准确的数字。
这是我当前的代码。我试过了get_posts
, get_pages
, 以及WP_query
. 它正在返回postID
在实际子查询中为\'0\' 并返回所有页面作为计数。我是否错过了一些非常明显的东西?
$query = new WP_Query(array(
\'post_type\' => \'location\',
\'posts_per_page\' => -1,
\'post_parent\' => 0,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
));
$locations = \'\';
while ($query->have_posts()) {
$query->the_post();
$post_id = $post->ID;
$title = get_the_title($post_id);
$permalink = get_the_permalink($post_id);
$locationdata = get_field(\'location\',$post_id);
$latitude = $locationdata[\'lat\'];
$longitude = $locationdata[\'lng\'];
$address = $locationdata[\'address\'];
$hours = get_field(\'hours\',$post_id);
$counter = new WP_Query(array( \'post_parent\' => $query->$post->ID, \'post_type\' => \'location\', \'posts_per_page\' => -1 ) );
$countchildren = $counter->post_count;
$mapslocations = $mapslocations.\'[new google.maps.LatLng(\'.$latitude.\', \'.$longitude.\'), \\\'\'.$title.\'\\\', \\\'<address>\'.$address.\'</address>\\\',"\'. $permalink .\'","\'.$countchildren.\'"],\';
$locations = $locations.\'<li><h2><a href="\'.get_the_permalink().\'">\'.get_the_title().\'</a></li>\';
}
wp_reset_postdata();