获取子类别ID(帮助改进我的代码)

时间:2018-02-22 作者:BoyIsBadNews

我在这里硬编码的类别id,所以我可以拉在需要的类别和他们的职位类别。但理想情况下,我希望通过调用父类别来列出子类别和帖子来实现这一点。我的代码可以工作,但我不想这样做,我希望听到任何改进建议。我自己也在努力解决这个问题。

父类别为“winners-2018”。

<?php 
    $catz = array(6012,6013,6014,6015,6016);
    foreach($catz as $i=>$categ){
    $cat_archive = get_category ($categ);
    //var_dump($cat_archive);
    global $wp_query; 

    $args = array_merge( $wp_query->query_vars,array(       
        \'paged\'=>$paged,    
        \'posts_per_page\'=>10,
        \'post_type\'   => \'winners\',
        \'category_name\' => $cat_archive->slug,
    ));
    ?>
    <div class="outer">
        <div class="inner">             
            <div class="inside">
                <h1><?php echo $cat_archive->name; ?></h1>
            </div>          
            <div class="left-content winners_fullwidth <?php if(is_category()) { echo $cat_archive->slug;} ?>">
                <div class="wrap-content clearfix">    
                    <?php
                    $cposts = get_posts(array(
                        \'category\'=>$categ,
                        \'orderby\' => \'title\',
                        \'order\' => \'ASC\',
                        \'posts_per_page\'=>10,
                        \'post_type\'   => \'winners\',
                    ));
                    foreach ( $cposts as $post ) : setup_postdata( $post ); ?>
                    <div class="press-box">                                
                    <?php if ( has_post_thumbnail() ) { ?>  
                        <div class="art-image">
                            <a href="<?php echo get_permalink(); ?>" title="<?php the_title();?>"><?php the_post_thumbnail( \'square-blog\' ); ?></a> 
                        </div>
                        <?php } ?>                                
                        <div class="art-right">                                   
                            <h2><a class="perma" href="<?php echo get_permalink(); ?>"><?php the_title();?></a></h2>
                                    <?php the_excerpt();?>
                        </div> 
                    </div>
                <?php endforeach;?>
                <?php wp_reset_query(); ?>  
                </div><!-- wrap-content -->
            </div><!-- left-content -->
        </div>  
        <?php } ?>

2 个回复
最合适的回答,由SO网友:BoyIsBadNews 整理而成

我自己解决了这个问题

   <?php
$idObj = get_category_by_slug(\'winners-2018\'); 
$id = $idObj->term_id;
$args = array(
  \'orderby\' => \'name\',
  \'parent\' => $id,
  \'taxonomy\' => \'category\',
  \'hide_empty\' => 1 
  );
$categories = get_categories( $args );


        foreach($categories as $category){
        $cat_archive = get_category ($category);
        //var_dump($cat_archive);
        global $wp_query; 

        $args = array_merge( $wp_query->query_vars,array(       
            \'paged\'=>$paged,    
            \'posts_per_page\'=>10,
            \'post_type\'   => \'winners\',
            \'category_name\' => $cat_archive->slug,
        ));

        ?>
        <div class="outer">
            <div class="inner">             
                <div class="inside">
                    <h1><?php echo $cat_archive->name; ?></h1>

                </div>          
                <div class="left-content winners_fullwidth <?php if(is_category()) { echo $cat_archive->slug;} ?>">
                    <div class="wrap-content clearfix">    
                        <?php
                        $cposts = get_posts(array(
                            \'orderby\' => \'title\',
                            \'order\' => \'ASC\',
                            \'posts_per_page\'=>10,
                            \'post_type\'   => \'winners\',
                            \'category_name\' => $cat_archive->slug,
                        ));
                        foreach ( $cposts as $post ) : setup_postdata( $post ); ?>
                        <div class="press-box">                                
                        <?php if ( has_post_thumbnail() ) { ?>  
                            <div class="art-image">
                                <a href="<?php echo get_permalink(); ?>" title="<?php the_title();?>"><?php the_post_thumbnail( \'square-blog\' ); ?></a> 
                            </div>
                            <?php } ?>                                
                            <div class="art-right">                                   
                                <h2><a class="perma" href="<?php echo get_permalink(); ?>"><?php the_title();?></a></h2>
                                <?php the_excerpt();?>
                            </div> 
                        </div>
                    <?php endforeach;?>
                    <?php wp_reset_query(); ?>  
                    </div><!-- wrap-content -->
                </div><!-- left-content -->
            </div>  
            <?php } ?>
Solution was

    $idObj = get_category_by_slug(\'winners-2018\'); 
$id = $idObj->term_id;
$args = array(
  \'orderby\' => \'name\',
  \'parent\' => $id,
  \'taxonomy\' => \'category\',
  \'hide_empty\' => 1 
  );
$categories = get_categories( $args );


        foreach($categories as $category){
        $cat_archive = get_category ($category);
        //var_dump($cat_archive);
        global $wp_query; 

        $args = array_merge( $wp_query->query_vars,array(       
            \'paged\'=>$paged,    
            \'posts_per_page\'=>10,
            \'post_type\'   => \'winners\',
            \'category_name\' => $cat_archive->slug,
        ));

SO网友:ahendwh2

从父类别slug开始,可以使用以下代码获取子类别:

$parent = get_category_by_slug(\'winners-2018\');

$catz = get_categories(
    array( \'parent\' => $parent->cat_ID )
);

foreach ($catz as $cat_archive) {
    // No need for get_category(); because get_categories() already returns category objects
    ...
}

结束

相关推荐

按帖子类型筛选wp_Dropdown_Categories

我正在使用wp\\u dropdown\\u categories向媒体库添加类别过滤器。它工作正常,除了显示分配给帖子的所有类别,但我希望它只显示分配给附件的类别。如果只为附件更新计数也很好。下面是将类别添加到附件并允许您对其进行筛选的代码。/* Add categories to attachments/media library */ function wptp_add_categories_to_attachments() { register_taxonomy_for_objec