wp_list_categories
函式
wp_list_categories ( $args = '' )
- 引數
-
-
(array|string)
$args
{ Array of optional arguments. See get_categories(), get_terms(), and WP_Term_Query::__construct() for information on additional accepted arguments. @type int|int[] $current_category ID of category, or array of IDs of categories, that should get the 'current-cat' class. Default 0. @type int $depth Category depth. Used for tab indentation. Default 0. @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their bool equivalents. Default 1. @type int[]|string $exclude Array or comma/space-separated string of term IDs to exclude. If `$hierarchical` is true, descendants of `$exclude` terms will also be excluded; see `$exclude_tree`. See get_terms(). Default empty string. @type int[]|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along with their descendants. See get_terms(). Default empty string. @type string $feed Text to use for the feed link. Default 'Feed for all posts filed under [cat name]'. @type string $feed_image URL of an image to use for the feed link. Default empty string. @type string $feed_type Feed type. Used to build feed link. See get_term_feed_link(). Default empty string (default feed). @type bool $hide_title_if_empty Whether to hide the `$title_li` element if there are no terms in the list. Default false (title will always be shown). @type string $separator Separator between links. Default '<br />'. @type bool|int $show_count Whether to include post counts. Accepts 0, 1, or their bool equivalents. Default 0. @type string $show_option_all Text to display for showing all categories. Default empty string. @type string $show_option_none Text to display for the 'no categories' option. Default 'No categories'. @type string $style The style used to display the categories list. If 'list', categories will be output as an unordered list. If left empty or another value, categories will be output separated by `<br>` tags. Default 'list'. @type string $taxonomy Name of the taxonomy to retrieve. Default 'category'. @type string $title_li Text to use for the list title `<li>` element. Pass an empty string to disable. Default 'Categories'. @type bool|int $use_desc_for_title Whether to use the category description as the title attribute. Accepts 0, 1, or their bool equivalents. Default 0. @type Walker $walker Walker object to use to build the output. Default empty which results in a Walker_Category instance being used. }
- Required: 否
- Default: (empty)
-
(array|string)
$args
{ Array of optional arguments. See get_categories(), get_terms(), and WP_Term_Query::__construct() for information on additional accepted arguments. @type int|int[] $current_category ID of category, or array of IDs of categories, that should get the 'current-cat' class. Default 0. @type int $depth Category depth. Used for tab indentation. Default 0. @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their bool equivalents. Default 1. @type int[]|string $exclude Array or comma/space-separated string of term IDs to exclude. If `$hierarchical` is true, descendants of `$exclude` terms will also be excluded; see `$exclude_tree`. See get_terms(). Default empty string. @type int[]|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along with their descendants. See get_terms(). Default empty string. @type string $feed Text to use for the feed link. Default 'Feed for all posts filed under [cat name]'. @type string $feed_image URL of an image to use for the feed link. Default empty string. @type string $feed_type Feed type. Used to build feed link. See get_term_feed_link(). Default empty string (default feed). @type bool $hide_title_if_empty Whether to hide the `$title_li` element if there are no terms in the list. Default false (title will always be shown). @type string $separator Separator between links. Default '<br />'. @type bool|int $show_count Whether to include post counts. Accepts 0, 1, or their bool equivalents. Default 0. @type string $show_option_all Text to display for showing all categories. Default empty string. @type string $show_option_none Text to display for the 'no categories' option. Default 'No categories'. @type string $style The style used to display the categories list. If 'list', categories will be output as an unordered list. If left empty or another value, categories will be output separated by `<br>` tags. Default 'list'. @type string $taxonomy Name of the taxonomy to retrieve. Default 'category'. @type string $title_li Text to use for the list title `<li>` element. Pass an empty string to disable. Default 'Categories'. @type bool|int $use_desc_for_title Whether to use the category description as the title attribute. Accepts 0, 1, or their bool equivalents. Default 0. @type Walker $walker Walker object to use to build the output. Default empty which results in a Walker_Category instance being used. }
- 返回值
-
- (void|string|false) Void if 'echo' argument is true, HTML list of categories if 'echo' is false. False if the taxonomy does not exist.
- 定義位置
-
- wp-includes/category-template.php , line 538
- 引入
- 2.1.0
- 棄用
- -
wp_list_categories: 這個函式檢索並顯示一個特定分類法的類別列表。你可以使用各種引數來定製輸出,如隱藏或顯示空的類別,顯示每個類別的文章數量,以及對列表進行排序。
顯示或檢索分類的HTML列表。
function wp_list_categories( $args = '' ) {
$defaults = array(
'child_of' => 0,
'current_category' => 0,
'depth' => 0,
'echo' => 1,
'exclude' => '',
'exclude_tree' => '',
'feed' => '',
'feed_image' => '',
'feed_type' => '',
'hide_empty' => 1,
'hide_title_if_empty' => false,
'hierarchical' => true,
'order' => 'ASC',
'orderby' => 'name',
'separator' => '<br />',
'show_count' => 0,
'show_option_all' => '',
'show_option_none' => __( 'No categories' ),
'style' => 'list',
'taxonomy' => 'category',
'title_li' => __( 'Categories' ),
'use_desc_for_title' => 0,
);
$parsed_args = wp_parse_args( $args, $defaults );
if ( ! isset( $parsed_args['pad_counts'] ) && $parsed_args['show_count'] && $parsed_args['hierarchical'] ) {
$parsed_args['pad_counts'] = true;
}
// Descendants of exclusions should be excluded too.
if ( true == $parsed_args['hierarchical'] ) {
$exclude_tree = array();
if ( $parsed_args['exclude_tree'] ) {
$exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $parsed_args['exclude_tree'] ) );
}
if ( $parsed_args['exclude'] ) {
$exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $parsed_args['exclude'] ) );
}
$parsed_args['exclude_tree'] = $exclude_tree;
$parsed_args['exclude'] = '';
}
if ( ! isset( $parsed_args['class'] ) ) {
$parsed_args['class'] = ( 'category' === $parsed_args['taxonomy'] ) ? 'categories' : $parsed_args['taxonomy'];
}
if ( ! taxonomy_exists( $parsed_args['taxonomy'] ) ) {
return false;
}
$show_option_all = $parsed_args['show_option_all'];
$show_option_none = $parsed_args['show_option_none'];
$categories = get_categories( $parsed_args );
$output = '';
if ( $parsed_args['title_li'] && 'list' === $parsed_args['style']
&& ( ! empty( $categories ) || ! $parsed_args['hide_title_if_empty'] )
) {
$output = '<li class="' . esc_attr( $parsed_args['class'] ) . '">' . $parsed_args['title_li'] . '<ul>';
}
if ( empty( $categories ) ) {
if ( ! empty( $show_option_none ) ) {
if ( 'list' === $parsed_args['style'] ) {
$output .= '<li class="cat-item-none">' . $show_option_none . '</li>';
} else {
$output .= $show_option_none;
}
}
} else {
if ( ! empty( $show_option_all ) ) {
$posts_page = '';
// For taxonomies that belong only to custom post types, point to a valid archive.
$taxonomy_object = get_taxonomy( $parsed_args['taxonomy'] );
if ( ! in_array( 'post', $taxonomy_object->object_type, true ) && ! in_array( 'page', $taxonomy_object->object_type, true ) ) {
foreach ( $taxonomy_object->object_type as $object_type ) {
$_object_type = get_post_type_object( $object_type );
// Grab the first one.
if ( ! empty( $_object_type->has_archive ) ) {
$posts_page = get_post_type_archive_link( $object_type );
break;
}
}
}
// Fallback for the 'All' link is the posts page.
if ( ! $posts_page ) {
if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) {
$posts_page = get_permalink( get_option( 'page_for_posts' ) );
} else {
$posts_page = home_url( '/' );
}
}
$posts_page = esc_url( $posts_page );
if ( 'list' === $parsed_args['style'] ) {
$output .= "<li class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>";
} else {
$output .= "<a href='$posts_page'>$show_option_all</a>";
}
}
if ( empty( $parsed_args['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
$current_term_object = get_queried_object();
if ( $current_term_object && $parsed_args['taxonomy'] === $current_term_object->taxonomy ) {
$parsed_args['current_category'] = get_queried_object_id();
}
}
if ( $parsed_args['hierarchical'] ) {
$depth = $parsed_args['depth'];
} else {
$depth = -1; // Flat.
}
$output .= walk_category_tree( $categories, $depth, $parsed_args );
}
if ( $parsed_args['title_li'] && 'list' === $parsed_args['style']
&& ( ! empty( $categories ) || ! $parsed_args['hide_title_if_empty'] )
) {
$output .= '</ul></li>';
}
/**
* Filters the HTML output of a taxonomy list.
*
* @since 2.1.0
*
* @param string $output HTML output.
* @param array|string $args An array or query string of taxonomy-listing arguments. See
* wp_list_categories() for information on accepted arguments.
*/
$html = apply_filters( 'wp_list_categories', $output, $args );
if ( $parsed_args['echo'] ) {
echo $html;
} else {
return $html;
}
}
常見問題
FAQs-
如何使用get_users獲取所有使用者列表?
要使用`
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`函式獲取使用者列表。 - 使用可選的過濾引數,根據需要過濾使用者列表。
- 使用迴圈遍歷使用者列表,並訪問每個使用者的屬性。
- 呼叫`
-
如何使用WP_PLUGIN_DIR和WP_PLUGIN_URL定義外掛的目錄和URL?
在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中新增自定義功能?
使用PHP在WordPress中新增自定義功能可以通過以下方式實現:
- 建立一個自定義外掛,將功能程式碼新增到外掛中。
- 使用WordPress的鉤子系統,將自定義功能繫結到適當的鉤子上。
- 在主題檔案中新增自定義功能程式碼,例如functions.php檔案。
- 使用WordPress的短程式碼功能,將自定義功能封裝為短程式碼。
- 使用WordPress的外掛或主題編輯器,將自定義功能程式碼新增到適當的位置。
下面是一個實操示例。
要在WordPress中新增自定義功能,可以按照以下步驟使用PHP編寫並新增自定義功能:
- 開啟WordPress安裝目錄中的主題資料夾,通常位於wp-content/themes/下,找到正在使用的主題資料夾。
- 在主題資料夾中,可以建立一個名為functions.php的檔案。如果該檔案已經存在,可以直接編輯它。
- 在functions.php檔案中,可以使用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主題資料夾中。
請注意,修改主題檔案可以在主題更新時丟失,因此建議在進行任何更改之前備份functions.php檔案。此外,為避免不必要的錯誤和衝突,建議在新增自定義功能前先了解WordPress開發文件和最佳實踐,以確保正確、安全地實現所需的自定義功能。
-
如何進行WordPress資料庫查詢優化?
-
如何使用do_action來觸發一個鉤子函式?
使用
do_action函式可以觸發一個鉤子函式。do_action函式的引數與要觸發的鉤子函式的引數相同。例如,觸發save_post鉤子函式的程式碼如下:
do_action( 'save_post', $post_ID, $post );
這裡,
$post_ID和$post是傳遞給鉤子函式的引數。 -
如何使用wp_get_current_user獲取當前登入使用者的資訊?
使用
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;
