admin管理员组文章数量:1295634
I have a custom post type and each post in this custom post type has many childs. I have a code that show every child that is child of the parent in child's page. (parent: post 1 , childs: sub1-post, sub2-post. in sub1-post shows the sub1-post and sub2-post title and permalink) the code is:
<?php
if ( $post->post_parent ) {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->post_parent,
'echo' => 0,
'post_type' => 'mycustom'
) );
} else {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->ID,
'echo' => 0,
'post_type' => 'mycustom'
) );
}
if ( $children ) : ?>
<ul>
<?php echo $children; ?>
</ul>
<?php endif; ?>
Above code works perfect. now i want this code to show the list of children in a select and option mode. Like:
<select>
<option>sub1-post</option>
<option>sub2-post</option>
</select>
But i want to echo dynamically. (like echo $children
but in options).
Hope described well.
I have a custom post type and each post in this custom post type has many childs. I have a code that show every child that is child of the parent in child's page. (parent: post 1 , childs: sub1-post, sub2-post. in sub1-post shows the sub1-post and sub2-post title and permalink) the code is:
<?php
if ( $post->post_parent ) {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->post_parent,
'echo' => 0,
'post_type' => 'mycustom'
) );
} else {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->ID,
'echo' => 0,
'post_type' => 'mycustom'
) );
}
if ( $children ) : ?>
<ul>
<?php echo $children; ?>
</ul>
<?php endif; ?>
Above code works perfect. now i want this code to show the list of children in a select and option mode. Like:
<select>
<option>sub1-post</option>
<option>sub2-post</option>
</select>
But i want to echo dynamically. (like echo $children
but in options).
Hope described well.
Share Improve this question edited Apr 17, 2021 at 12:33 Elex 1,0451 gold badge7 silver badges15 bronze badges asked Apr 16, 2021 at 13:03 saeedsaeed 156 bronze badges1 Answer
Reset to default 1Try to use wp_dropdown_pages instead of wp_list_pages .
This function does almost the same, but returns a dropdown, instead of list items. (do not need to echo select opening and closing tags separately)
if ( $post->post_parent ) {
$children = wp_dropdown_pages( array(
'name' => 'your-name' // paste field name here
'child_of' => $post->post_parent,
'post_type' => 'mycustom',
'echo' => 0
));
} else {
$children = wp_dropdown_pages( array(
'name' => 'your-name' // paste field name here
'child_of' => $post->ID,
'post_type' => 'mycustom',
'echo' => 0
));
}
if( $children ):
echo $children;
endif;
Hope you already know that select is a form element and it keeps post id as option value, so you need to solve this manually if you want to redirect users to selected page id. (Code is not tested)
wp_dropdown_pages Code Reference
本文标签: phpHow to show childs of certain custom post type in option list
版权声明:本文标题:php - How to show childs of certain custom post type in option list? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741601576a2387732.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论