get_categories()
最后更新于:2021-11-26 06:59:20
get_categories( string|array$args=”)Retrieves a list of category objects.
参数
- $args
-
(string|array) (Optional) Arguments to retrieve categories. See get_terms() for additional options.
-
‘taxonomy’
(string) Taxonomy to retrieve terms for. Default ‘category’.
Default value: ”
-
‘taxonomy’
响应
(array) List of category objects.
源文件
文件: gc-includes/category.php
function get_categories( $args = '' ) {
$defaults = array( 'taxonomy' => 'category' );
$args = gc_parse_args( $args, $defaults );
/**
* Filters the taxonomy used to retrieve terms when calling get_categories().
*
* @since 2.7.0
*
* @param string $taxonomy Taxonomy to retrieve terms from.
* @param array $args An array of arguments. See get_terms().
*/
$args['taxonomy'] = apply_filters( 'get_categories_taxonomy', $args['taxonomy'], $args );
// Back compat.
if ( isset( $args['type'] ) && 'link' === $args['type'] ) {
_deprecated_argument(
__FUNCTION__,
'3.0.0',
sprintf(
/* translators: 1: "type => link", 2: "taxonomy => link_category" */
__( '%1$s is deprecated. Use %2$s instead.' ),
'<code>type => link</code>',
'<code>taxonomy => link_category</code>'
)
);
$args['taxonomy'] = 'link_category';
}
$categories = get_terms( $args );
if ( is_gc_error( $categories ) ) {
$categories = array();
} else {
$categories = (array) $categories;
foreach ( array_keys( $categories ) as $k ) {
_make_cat_compat( $categories[ $k ] );
}
}
return $categories;
}
<?php $categories = get_categories( array( 'orderby' => 'name', 'order' => 'ASC' ) ); foreach( $categories as $category ) { $category_link = sprintf( '<a href="https://docs.gechiui.com/functions/get_categories/%1$s" alt="%2$s">%3$s</a>', esc_url( get_category_link( $category->term_id ) ), esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ), esc_html( $category->name ) ); echo '<p>' . sprintf( esc_html__( 'Category: %s', 'textdomain' ), $category_link ) . '</p> '; echo '<p>' . sprintf( esc_html__( 'Description: %s', 'textdomain' ), $category->description ) . '</p>'; echo '<p>' . sprintf( esc_html__( 'Post Count: %s', 'textdomain' ), $category->count ) . '</p>'; }
<div class="category-menu-container"> <ul class="category-menu"> <li class="unstyled"> <h4 class="category-menu-heading"> <?php echo get_cat_name( $category_id = 130 );?> </h4> </li> <?php $categories = get_categories( array( 'orderby' => 'name', 'order' => 'ASC', 'parent' => 130, ) ); foreach( $categories as $category ) { $category_link = sprintf( '<a href="https://docs.gechiui.com/functions/get_categories/%1$s" alt="%2$s">%3$s</a>', esc_url( get_category_link( $category->term_id ) ), esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ), esc_html( $category->name ) ); echo '<p>' . sprintf( esc_html__( '%s', 'textdomain' ), $category_link ) . '</p> '; } ?> </ul> </div>
<select name="event-dropdown"> <option value=""><?php echo esc_attr_e( 'Select Event', 'textdomain' ); ?></option> <?php $categories = get_categories( array( 'child_of' => 10 ) ); foreach ( $categories as $category ) { printf( '<option value="%1$s">%2$s (%3$s)</option>', esc_attr( '/category/archives/' . $category->category_nicename ), esc_html( $category->cat_name ), esc_html( $category->category_count ) ); } ?> </select>