我有一个下拉菜单,显示一个链接(永久链接),指向自定义帖子类型(county)的所有帖子,并且只想显示他们的孩子。这是我到目前为止的代码,但get\\u children不起作用。。。
<ul>
<?php $menu = new WP_Query( array(
\'post_type\' => \'county\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'order\' => \'desc\'
) );
while ( $menu->have_posts() ) : $menu->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<ul>
<?php get_children(); ?>
</ul>
</li>
<?php endwhile; ?>
</ul>
这是允许我将自定义帖子类型指定为另一个帖子类型的父级的代码
function show_parent_metabox() {
parent_select(\'county\');
}
function parent_select ($parent_type) {
global $post;
global $wpdb;
$query = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = \'{$parent_type}\' AND post_status = \'publish\' ORDER BY post_title";
$results = $wpdb->get_results($query, OBJECT);
echo \'<select name="parent_id" id="parent_id">\';
echo \'<option value = "">None</option>\';
foreach ($results as $r) {
echo \'<option value="\', $r->ID, \'"\', $r->ID == $post->post_parent ? \' selected="selected"\' : \'\', \'>\', $r->post_title, \'</option>\';
}
echo \'</select>\';
}
SO网友:Kumar
以下是可用于获取子自定义帖子的一般逻辑
我在这里假设的是,你创建一个county
发布,然后创建area
发布并从父元数据库中选择所需的county
发布并将发布id保存在post_parent
function wpse_128630_cp( $post_type = \'county\', $post_parent = \'\', $posts_per_page = -1 ){
$args = array(
\'post_type\' => $post_type,
\'post_status\' => \'publish\',
\'posts_per_page\' => $posts_per_page,
\'post_parent\' => $post_parent ,
\'order\' => \'desc\'
);
$menu = new WP_Query( $args );
if(!empty($menu)) { return $menu;}
else{
//To debug, uncomment below code
//echo "<pre>";
//print_r($menu);
//echo "</pre>";
}
}
<ul class="wpse-parent menu">
<?php $menu = wpse_128630_cp();
while ( $menu->have_posts() ) : $menu->the_post(); ?>
<li class="menu-item">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php
$submenu = wpse_128630_cp( \'area\', get_the_ID() );
if(!empty($submenu)){ ?>
<ul class="wpse-sub-menu menu"> <?php
while ( $submenu->have_posts() ) : $submenu->the_post(); ?>
<li class="menu-item">
<a href="<?php $submenu->the_permalink(); ?>"><?php $submenu->the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul><?php
}?>
</li>
<?php endwhile; ?>
</ul>