wordpress分类目录函数get_categories参数说明

Home / Article MrLee 2016-4-9 4032

刚想把首页的TAB栏内容再加几条,我原先是通过parent参数来获取本站IT专题下面所有的子分类,代码如下:
$categories = get_categories(array('parent'=>'979'));

不过这样有个弊端,如果想再添加其它的栏目就不比较麻烦。于是查了下该函数的参数,里面有一个include值,只要传入栏目的ID即可。具体参数如下
参数说明
type (字符)post和link 其中link在新版3.0以后已被弃用。 child_of (整数)仅显示标注了编号的分类的子类。该参数无默认值。使用该参数时应将hide_empty参数设为false parent (整数)只显示某个父级分类以及下面的子分类(注:子分类只显示一个层级)。 orderby (字符)将分类按字母顺序或独有分类编号进行排序。默认为按分类 编号排序包括ID(默认)和Name order (字符)为类别排序(升序或降序)。默认升序。可能的值包括asc(默认)和desc hide_empty (布尔值)触发显示没有文章的分类。默认值为true(隐藏空类别)。有效的值包括:1(true)和0(false)。 hierarchical (布尔值)将子类作为内部列表项目(父列表项下)的层级关系。默认为true(显示父列表项下的子类)。有效值包括1 (true)和0(false) exclude (字符)除去分类列表中一个或多个分类,多个可以用逗号分开,用分类ID号表示。 include (字符)只包含指定分类ID编号的分类。多个可以用逗号分开,用分类ID号表示。 pad_counts (布尔值)通过子类中的项来计算链接或文章。有效值包括1(true)和0(false),0为默认。 number (字符)将要返回的类别数量。 taxonomy (字符)返回一个分类法,这个是wordpress3.0版本后新添加的一个参数。返回的值包括category(默认)和taxonomy(一些新定义的分类名称)。
看官网的例子
function wp_list_categories( $args = '' ) {
    $defaults = array(
        'show_option_all' => '', 'show_option_none' => __('No categories'),
        'orderby' => 'name', 'order' => 'ASC',
        'style' => 'list',
        'show_count' => 0, 'hide_empty' => 1,
        'use_desc_for_title' => 1, 'child_of' => 0,
        'feed' => '', 'feed_type' => '',
        'feed_image' => '', 'exclude' => '',
        'exclude_tree' => '', 'current_category' => 0,
        'hierarchical' => true, 'title_li' => __( 'Categories' ),
        'hide_title_if_empty' => false,
        'echo' => 1, 'depth' => 0,
        'separator' => '
',
        'taxonomy' => 'category'
    );
 
    $r = wp_parse_args( $args, $defaults );
 
    if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
        $r['pad_counts'] = true;
 
    // Descendants of exclusions should be excluded too.
    if ( true == $r['hierarchical'] ) {
        $exclude_tree = array();
 
        if ( $r['exclude_tree'] ) {
            $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude_tree'] ) );
        }
 
        if ( $r['exclude'] ) {
            $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude'] ) );
        }
 
        $r['exclude_tree'] = $exclude_tree;
        $r['exclude'] = '';
    }
 
    if ( ! isset( $r['class'] ) )
        $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
 
    if ( ! taxonomy_exists( $r['taxonomy'] ) ) {
        return false;
    }
 
    $show_option_all = $r['show_option_all'];
    $show_option_none = $r['show_option_none'];
 
    $categories = get_categories( $r );
 
    $output = '';
    if ( $r['title_li'] && 'list' == $r['style'] && ( ! empty( $categories ) || ! $r['hide_title_if_empty'] ) ) {
        $output = '
  • ' . $r['title_li'] . '
      '; } if ( empty( $categories ) ) { if ( ! empty( $show_option_none ) ) { if ( 'list' == $r['style'] ) { $output .= '
    • ' . $show_option_none . '
    • '; } 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( $r['taxonomy'] ); if ( ! in_array( 'post', $taxonomy_object->object_type ) && ! in_array( 'page', $taxonomy_object->object_type ) ) { 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' == $r['style'] ) { $output .= "
    • $show_option_all
    • "; } else { $output .= "$show_option_all"; } } if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) { $current_term_object = get_queried_object(); if ( $current_term_object && $r['taxonomy'] === $current_term_object->taxonomy ) { $r['current_category'] = get_queried_object_id(); } } if ( $r['hierarchical'] ) { $depth = $r['depth']; } else { $depth = -1; // Flat. } $output .= walk_category_tree( $categories, $depth, $r ); } if ( $r['title_li'] && 'list' == $r['style'] ) $output .= '
  • '; /** * Filter the HTML output of a taxonomy list. * * @since 2.1.0 * * @param string $output HTML output. * @param array $args An array of taxonomy-listing arguments. */ $html = apply_filters( 'wp_list_categories', $output, $args ); if ( $r['echo'] ) { echo $html; } else { return $html; } }

     

    本文链接:https://www.it72.com/8980.htm

    推荐阅读
    最新回复 (0)
    返回