parent_dropdown

函数
parent_dropdown ( $default_page = 0, $parent_page = 0, $level = 0, $post = null )
参数
  • (int) $default_page Optional. The default page ID to be pre-selected. Default 0.
    Required:
  • (int) $parent_page Optional. The parent page ID. Default 0.
    Required:
  • (int) $level Optional. Page depth level. Default 0.
    Required:
  • (int|WP_Post) $post Post ID or WP_Post object.
    Required:
    Default: null
返回值
  • (void|false) Void on success, false if the page has no children.
定义位置
相关方法
page_template_dropdownwp_dropdown_catswp_dropdown_userswalk_page_dropdown_treewp_dropdown_roles
引入
1.5.0
弃用
-

parent_dropdown: 这个函数在一个分层的文章类型中生成一个父文章的下拉列表。它需要三个参数。$post_type (string)表示文章类型,$parent (int)表示所选父级的ID,$args (array)表示附加参数。

打印出页面父级下拉的选项HTML元素。

function parent_dropdown( $default_page = 0, $parent_page = 0, $level = 0, $post = null ) {
	global $wpdb;

	$post  = get_post( $post );
	$items = $wpdb->get_results(
		$wpdb->prepare(
			"SELECT ID, post_parent, post_title
			FROM $wpdb->posts
			WHERE post_parent = %d AND post_type = 'page'
			ORDER BY menu_order",
			$parent_page
		)
	);

	if ( $items ) {
		foreach ( $items as $item ) {
			// A page cannot be its own parent.
			if ( $post && $post->ID && (int) $item->ID === $post->ID ) {
				continue;
			}

			$pad      = str_repeat( ' ', $level * 3 );
			$selected = selected( $default_page, $item->ID, false );

			echo "nt<option class='level-$level' value='$item->ID' $selected>$pad " . esc_html( $item->post_title ) . '</option>';
			parent_dropdown( $default_page, $item->ID, $level + 1 );
		}
	} else {
		return false;
	}
}

常见问题

FAQs
查看更多 >