wp_nav_menu_item_post_type_meta_box ( $data_object, $box )
wp_nav_menu_item_post_type_meta_box: 这个钩子用来在WordPress导航菜单项目编辑屏幕上添加一个元框,允许用户选择一个文章类型。
显示一个特定组的贡献者列表。
function wp_nav_menu_item_post_type_meta_box( $data_object, $box ) { global $_nav_menu_placeholder, $nav_menu_selected_id; $post_type_name = $box['args']->name; $post_type = get_post_type_object( $post_type_name ); $tab_name = $post_type_name . '-tab'; // Paginate browsing for large numbers of post objects. $per_page = 50; $pagenum = isset( $_REQUEST[ $tab_name ] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1; $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0; $args = array( 'offset' => $offset, 'order' => 'ASC', 'orderby' => 'title', 'posts_per_page' => $per_page, 'post_type' => $post_type_name, 'suppress_filters' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, ); if ( isset( $box['args']->_default_query ) ) { $args = array_merge( $args, (array) $box['args']->_default_query ); } /* * If we're dealing with pages, let's prioritize the Front Page, * Posts Page and Privacy Policy Page at the top of the list. */ $important_pages = array(); if ( 'page' === $post_type_name ) { $suppress_page_ids = array(); // Insert Front Page or custom Home link. $front_page = 'page' === get_option( 'show_on_front' ) ? (int) get_option( 'page_on_front' ) : 0; $front_page_obj = null; if ( ! empty( $front_page ) ) { $front_page_obj = get_post( $front_page ); $front_page_obj->front_or_home = true; $important_pages[] = $front_page_obj; $suppress_page_ids[] = $front_page_obj->ID; } else { $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? (int) $_nav_menu_placeholder - 1 : -1; $front_page_obj = (object) array( 'front_or_home' => true, 'ID' => 0, 'object_id' => $_nav_menu_placeholder, 'post_content' => '', 'post_excerpt' => '', 'post_parent' => '', 'post_title' => _x( 'Home', 'nav menu home label' ), 'post_type' => 'nav_menu_item', 'type' => 'custom', 'url' => home_url( '/' ), ); $important_pages[] = $front_page_obj; } // Insert Posts Page. $posts_page = 'page' === get_option( 'show_on_front' ) ? (int) get_option( 'page_for_posts' ) : 0; if ( ! empty( $posts_page ) ) { $posts_page_obj = get_post( $posts_page ); $posts_page_obj->posts_page = true; $important_pages[] = $posts_page_obj; $suppress_page_ids[] = $posts_page_obj->ID; } // Insert Privacy Policy Page. $privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' ); if ( ! empty( $privacy_policy_page_id ) ) { $privacy_policy_page = get_post( $privacy_policy_page_id ); if ( $privacy_policy_page instanceof WP_Post && 'publish' === $privacy_policy_page->post_status ) { $privacy_policy_page->privacy_policy_page = true; $important_pages[] = $privacy_policy_page; $suppress_page_ids[] = $privacy_policy_page->ID; } } // Add suppression array to arguments for WP_Query. if ( ! empty( $suppress_page_ids ) ) { $args['post__not_in'] = $suppress_page_ids; } } // @todo Transient caching of these results with proper invalidation on updating of a post of this type. $get_posts = new WP_Query; $posts = $get_posts->query( $args ); // Only suppress and insert when more than just suppression pages available. if ( ! $get_posts->post_count ) { if ( ! empty( $suppress_page_ids ) ) { unset( $args['post__not_in'] ); $get_posts = new WP_Query; $posts = $get_posts->query( $args ); } else { echo '<p>' . __( 'No items.' ) . '</p>'; return; } } elseif ( ! empty( $important_pages ) ) { $posts = array_merge( $important_pages, $posts ); } $num_pages = $get_posts->max_num_pages; $page_links = paginate_links( array( 'base' => add_query_arg( array( $tab_name => 'all', 'paged' => '%#%', 'item-type' => 'post_type', 'item-object' => $post_type_name, ) ), 'format' => '', 'prev_text' => '<span aria-label="' . esc_attr__( 'Previous page' ) . '">' . __( '«' ) . '</span>', 'next_text' => '<span aria-label="' . esc_attr__( 'Next page' ) . '">' . __( '»' ) . '</span>', 'before_page_number' => '<span class="screen-reader-text">' . __( 'Page' ) . '</span> ', 'total' => $num_pages, 'current' => $pagenum, ) ); $db_fields = false; if ( is_post_type_hierarchical( $post_type_name ) ) { $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID', ); } $walker = new Walker_Nav_Menu_Checklist( $db_fields ); $current_tab = 'most-recent'; if ( isset( $_REQUEST[ $tab_name ] ) && in_array( $_REQUEST[ $tab_name ], array( 'all', 'search' ), true ) ) { $current_tab = $_REQUEST[ $tab_name ]; } if ( ! empty( $_REQUEST[ 'quick-search-posttype-' . $post_type_name ] ) ) { $current_tab = 'search'; } $removed_args = array( 'action', 'customlink-tab', 'edit-menu-item', 'menu-item', 'page-tab', '_wpnonce', ); $most_recent_url = ''; $view_all_url = ''; $search_url = ''; if ( $nav_menu_selected_id ) { $most_recent_url = esc_url( add_query_arg( $tab_name, 'most-recent', remove_query_arg( $removed_args ) ) ); $view_all_url = esc_url( add_query_arg( $tab_name, 'all', remove_query_arg( $removed_args ) ) ); $search_url = esc_url( add_query_arg( $tab_name, 'search', remove_query_arg( $removed_args ) ) ); } ?> <div id="posttype-<?php echo $post_type_name; ?>" class="posttypediv"> <ul id="posttype-<?php echo $post_type_name; ?>-tabs" class="posttype-tabs add-menu-item-tabs"> <li <?php echo ( 'most-recent' === $current_tab ? ' class="tabs"' : '' ); ?>> <a class="nav-tab-link" data-type="tabs-panel-posttype-<?php echo esc_attr( $post_type_name ); ?>-most-recent" href="<?php echo $most_recent_url; ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent"> <?php _e( 'Most Recent' ); ?> </a> </li> <li <?php echo ( 'all' === $current_tab ? ' class="tabs"' : '' ); ?>> <a class="nav-tab-link" data-type="<?php echo esc_attr( $post_type_name ); ?>-all" href="<?php echo $view_all_url; ?>#<?php echo $post_type_name; ?>-all"> <?php _e( 'View All' ); ?> </a> </li> <li <?php echo ( 'search' === $current_tab ? ' class="tabs"' : '' ); ?>> <a class="nav-tab-link" data-type="tabs-panel-posttype-<?php echo esc_attr( $post_type_name ); ?>-search" href="<?php echo $search_url; ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-search"> <?php _e( 'Search' ); ?> </a> </li> </ul><!-- .posttype-tabs --> <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent" class="tabs-panel <?php echo ( 'most-recent' === $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>" role="region" aria-label="<?php _e( 'Most Recent' ); ?>" tabindex="0"> <ul id="<?php echo $post_type_name; ?>checklist-most-recent" class="categorychecklist form-no-clear"> <?php $recent_args = array_merge( $args, array( 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => 15, ) ); $most_recent = $get_posts->query( $recent_args ); $args['walker'] = $walker; /** * Filters the posts displayed in the 'Most Recent' tab of the current * post type's menu items meta box. * * The dynamic portion of the hook name, `$post_type_name`, refers to the post type name. * * Possible hook names include: * * - `nav_menu_items_post_recent` * - `nav_menu_items_page_recent` * * @since 4.3.0 * @since 4.9.0 Added the `$recent_args` parameter. * * @param WP_Post[] $most_recent An array of post objects being listed. * @param array $args An array of `WP_Query` arguments for the meta box. * @param array $box Arguments passed to `wp_nav_menu_item_post_type_meta_box()`. * @param array $recent_args An array of `WP_Query` arguments for 'Most Recent' tab. */ $most_recent = apply_filters( "nav_menu_items_{$post_type_name}_recent", $most_recent, $args, $box, $recent_args ); echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $most_recent ), 0, (object) $args ); ?> </ul> </div><!-- /.tabs-panel --> <div class="tabs-panel <?php echo ( 'search' === $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>" id="tabs-panel-posttype-<?php echo $post_type_name; ?>-search" role="region" aria-label="<?php echo $post_type->labels->search_items; ?>" tabindex="0"> <?php if ( isset( $_REQUEST[ 'quick-search-posttype-' . $post_type_name ] ) ) { $searched = esc_attr( $_REQUEST[ 'quick-search-posttype-' . $post_type_name ] ); $search_results = get_posts( array( 's' => $searched, 'post_type' => $post_type_name, 'fields' => 'all', 'order' => 'DESC', ) ); } else { $searched = ''; $search_results = array(); } ?> <p class="quick-search-wrap"> <label for="quick-search-posttype-<?php echo $post_type_name; ?>" class="screen-reader-text"><?php _e( 'Search' ); ?></label> <input type="search"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="quick-search" value="<?php echo $searched; ?>" name="quick-search-posttype-<?php echo $post_type_name; ?>" id="quick-search-posttype-<?php echo $post_type_name; ?>" /> <span class="spinner"></span> <?php submit_button( __( 'Search' ), 'small quick-search-submit hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-posttype-' . $post_type_name ) ); ?> </p> <ul id="<?php echo $post_type_name; ?>-search-checklist" data-wp-lists="list:<?php echo $post_type_name; ?>" class="categorychecklist form-no-clear"> <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?> <?php $args['walker'] = $walker; echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $search_results ), 0, (object) $args ); ?> <?php elseif ( is_wp_error( $search_results ) ) : ?> <li><?php echo $search_results->get_error_message(); ?></li> <?php elseif ( ! empty( $searched ) ) : ?> <li><?php _e( 'No results found.' ); ?></li> <?php endif; ?> </ul> </div><!-- /.tabs-panel --> <div id="<?php echo $post_type_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php echo ( 'all' === $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>" role="region" aria-label="<?php echo $post_type->labels->all_items; ?>" tabindex="0"> <?php if ( ! empty( $page_links ) ) : ?> <div class="add-menu-item-pagelinks"> <?php echo $page_links; ?> </div> <?php endif; ?> <ul id="<?php echo $post_type_name; ?>checklist" data-wp-lists="list:<?php echo $post_type_name; ?>" class="categorychecklist form-no-clear"> <?php $args['walker'] = $walker; if ( $post_type->has_archive ) { $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? (int) $_nav_menu_placeholder - 1 : -1; array_unshift( $posts, (object) array( 'ID' => 0, 'object_id' => $_nav_menu_placeholder, 'object' => $post_type_name, 'post_content' => '', 'post_excerpt' => '', 'post_title' => $post_type->labels->archives, 'post_type' => 'nav_menu_item', 'type' => 'post_type_archive', 'url' => get_post_type_archive_link( $post_type_name ), ) ); } /** * Filters the posts displayed in the 'View All' tab of the current * post type's menu items meta box. * * The dynamic portion of the hook name, `$post_type_name`, refers * to the slug of the current post type. * * Possible hook names include: * * - `nav_menu_items_post` * - `nav_menu_items_page` * * @since 3.2.0 * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object. * * @see WP_Query::query() * * @param object[] $posts The posts for the current post type. Mostly `WP_Post` objects, but * can also contain "fake" post objects to represent other menu items. * @param array $args An array of `WP_Query` arguments. * @param WP_Post_Type $post_type The current post type object for this menu item meta box. */ $posts = apply_filters( "nav_menu_items_{$post_type_name}", $posts, $args, $post_type ); $checkbox_items = walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $posts ), 0, (object) $args ); echo $checkbox_items; ?> </ul> <?php if ( ! empty( $page_links ) ) : ?> <div class="add-menu-item-pagelinks"> <?php echo $page_links; ?> </div> <?php endif; ?> </div><!-- /.tabs-panel --> <p class="button-controls wp-clearfix" data-items-type="posttype-<?php echo esc_attr( $post_type_name ); ?>"> <span class="list-controls hide-if-no-js"> <input type="checkbox"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> id="<?php echo esc_attr( $tab_name ); ?>" class="select-all" /> <label for="<?php echo esc_attr( $tab_name ); ?>"><?php _e( 'Select All' ); ?></label> </span> <span class="add-to-menu"> <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-post-type-menu-item" id="<?php echo esc_attr( 'submit-posttype-' . $post_type_name ); ?>" /> <span class="spinner"></span> </span> </p> </div><!-- /.posttypediv --> <?php }
要使用` get_users
`函数获取所有用户列表,可以按照以下步骤进行:
1. 使用` get_users
`函数调用获取用户列表:
$users = get_users();
2. 您可以按需使用参数来过滤结果。例如,您可以通过角色、用户ID、用户登录名等过滤用户列表。以下是一个根据用户角色为过滤条件的示例:
$users = get_users( array( 'role' => 'subscriber' // 将角色名称替换为您要过滤的角色 ) );
在上述示例中,将` role
`参数设置为所需的角色名称来过滤用户列表。
3. 您可以使用循环遍历获取的用户列表,并访问每个用户的属性。例如,以下示例将显示每个用户的用户名和电子邮件地址:
foreach( $users as $user ) { echo '用户名:' . $user->user_login . ', 电子邮件:' . $user->user_email . ; }
在上述示例中,通过` $user->user_login
`和` $user->user_email
`访问每个用户的用户名和电子邮件地址。
请注意,` get_users
`函数默认返回所有用户,并可以根据需要使用更多参数进行过滤。您可以参阅WordPress官方文档中的` get_users
`函数文档,了解更多可用参数和用法示例。
总结起来,使用` get_users
`函数获取所有用户列表的步骤是:
get_users
`函数获取用户列表。在WordPress中,可以使用WP_PLUGIN_DIR和WP_PLUGIN_URL常量来定义插件的目录路径和URL。
1. `WP_PLUGIN_DIR`:这是一个常量,用于定义插件的目录路径(文件系统路径)。您可以使用以下代码在插件文件中访问该常量:
$plugin_dir = WP_PLUGIN_DIR . '/your-plugin-folder/';
在上述代码中,将"your-plugin-folder"替换为您插件的实际文件夹名称。使用该常量,您可以获取插件文件的完整路径。
2. `WP_PLUGIN_URL`:这是一个常量,用于定义插件的URL(用于在网页上访问插件文件)。以下是一个使用该常量的示例:
$plugin_url = WP_PLUGIN_URL . '/your-plugin-folder/';
同样,请将"your-plugin-folder"替换为您插件的实际文件夹名称。使用该常量,您可以获取插件在网页上的完整URL。
请注意,`WP_PLUGIN_DIR`和`WP_PLUGIN_URL`常量在WordPress版本2.6之后引入。从WordPress 5.5版本开始,这两个常量被标记为过时(deprecated),因为WordPress更倾向于使用新的插件文件结构。如果您正在开发新插件,建议使用新的插件文件结构和相关函数。
在新的插件文件结构中,可以使用以下函数来获取插件的目录路径和URL:
- `plugin_dir_path()`:获取插件目录路径。
- `plugin_dir_url()`:获取插件URL。
这些函数会自动将插件的版本、多站点和SSL等考虑因素纳入计算。
总结起来,使用`WP_PLUGIN_DIR`和`WP_PLUGIN_URL`常量定义插件的目录和URL的方法是:
$plugin_dir = WP_PLUGIN_DIR . '/your-plugin-folder/'; $plugin_url = WP_PLUGIN_URL . '/your-plugin-folder/';
但请注意,这两个常量已被标记为过时,建议使用新的插件文件结构和相关函数来获取插件的路径和URL。
使用PHP在WordPress中添加自定义功能可以通过以下方式实现:
下面是一个实操示例。
要在WordPress中添加自定义功能,可以按照以下步骤使用PHP编写并添加自定义功能:
// 添加自定义功能示例 // 1. 创建自定义短代码 function custom_shortcode() { return '这是我的自定义短代码内容'; } add_shortcode('custom', 'custom_shortcode'); // 2. 自定义小工具 function custom_widget() { echo '这是我的自定义小工具内容'; } register_widget('custom_widget'); // 3. 自定义菜单 function custom_menu() { register_nav_menu('custom-menu', '自定义菜单'); } add_action('after_setup_theme', 'custom_menu'); // 4. 自定义页面模板 function custom_page_template() { /* Template Name: 自定义模板 */ // 自定义模板的内容和样式 }
请注意,修改主题文件可以在主题更新时丢失,因此建议在进行任何更改之前备份functions.php文件。此外,为避免不必要的错误和冲突,建议在添加自定义功能前先了解WordPress开发文档和最佳实践,以确保正确、安全地实现所需的自定义功能。
使用 do_action
函数可以触发一个钩子函数。do_action
函数的参数与要触发的钩子函数的参数相同。
例如,触发save_post钩子函数的代码如下:
do_action( 'save_post', $post_ID, $post );
这里,$post_ID
和 $post
是传递给钩子函数的参数。
使用 wp_get_current_user
获取当前登录用户的信息:
$current_user = wp_get_current_user(); // 获取当前用户的ID $user_id = $current_user->ID; // 获取当前用户的用户名 $user_login = $current_user->user_login; // 获取当前用户的邮箱 $user_email = $current_user->user_email; // 获取当前用户的显示名称 $display_name = $current_user->display_name;