• I am building a new child theme and I came across a problem which I think you all can help me with. I am new to WordPress and is stuck at this point. I am using theme simone { https://wordpress.org/themes/simone } for this purpose.

    the problem is that I want to show sub-categories of parents till there is no sub-categories left and then on the last sub-category it should show the posts it contains. For that I have prepared two codes separately but when I combine them they give me error. Please help me with this. as separate code they work as I want. But when combined they give error.

    Code : 1 { for showing sub-categories.

    <ul>
    <?php
    	$cat = get_query_var('cat');
    	$category = get_category ($cat);
    
    	if ($category->cat_ID) {
    		$children = wp_list_categories("orderby=id&depth=0&hide_empty=0&title_li=&child_of=".$category->cat_ID."&echo=0");
    		// change depth to 1 if only the top level sub categories should be shown i.e., with children NOT expanded
    		if ($children) {
    			echo $children;
    		}
    
    	}
    ?>
    </ul>

    Code : 2 { for showing the posts under the sub-category }

    <ul>
    <?php
    global $post;
    $category = get_the_category($post->ID);
    $category = $category[0]->cat_ID;
    $myposts = get_posts(array('numberposts' => 20, 'offset' => 0, 'category__in' => array($category), 'post_status'=>'publish', 'order'=>'ASC' ));
    foreach($myposts as $post) :
    setup_postdata($post);
    ?>
    <li>
    <a href="<?php the_permalink(); ?>">
    <?php the_title(); ?></a>
    </li>
    <?php endforeach; ?>
    </ul>

    please help me combining these two codes so that they work properly.

The topic ‘How to use If, else condition in wordpress’ is closed to new replies.