0

I have here hardcoded category id so I can pull in needed categories and their posts by category. But ideally I would like to get this by calling parent category to list child and post. My code works but that's not how I would like to do it and would like to hear any suggestions to improve. I'm still trying to figure this out myself as well.

Parent category is '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 } ?>
0

Starting from your parent category slug you can use the following code to get the child categories:

$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
    ...
}
  • I edit the code but unfortunately didn't work @ahendwh2 – BoyIsBadNews Feb 22 '18 at 15:23
0

Managed to solve this issue myself

   <?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,
        ));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.