Need Help With php IF statement (WordPress Related)
The following is the code I'm trying to implement:
<?php if (is_category()) {
$this_category = get_category($cat);
if (get_category_children($this_category->cat_ID) != "") {
echo "<ul>";
wp_list_categories('orderby=id&depth=1&show_count= 0&title_li=
&use_desc_for_title=1&child_of='.$this_category->cat_ID);
echo "</ul>";
}
} ; ?>
<?php $children = $wpdb->get_results( "SELECT term_id FROM $wpdb->term_taxonomy WHERE parent=$category_id" );
$no_children = count($children); ?>
<?php if ($no_children == "0") { ?>
<?php $recent = new WP_Query("cat=$this_category->cat_ID, &orderby=id&depth=0"); while($recent->have_posts()) : $recent->the_post();?>
<ul>
<li><b><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></b></li>
</ul>
<?php endwhile; ?>
<?php } ?>
Now for the first part of the code
--------------------------------------------------------------------------------------------------------------------------------------
<?php if (is_category()) {
$this_category = get_category($cat);
if (get_category_children($this_category->cat_ID) != "") {
echo "<ul>";
wp_list_categories('orderby=id&depth=1&show_count= 0&title_li=
&use_desc_for_title=1&child_of='.$this_category->cat_ID);
echo "</ul>";
}
} ; ?>
--------------------------------------------------------------------------------------------------------------------------------------
This part of the code will show only the sub categories on the category page of the parent category.
My site structure is
Category>> Sub Category >>Sub Category >> Post
Notice that the post is filed under the second level Sub Category.
This code is working fine. So when I click on the category name, I go to the category page listing all the Sub Categories on that page. Now I again click on a particular Sub Category and again I get Sub Categories (2nd Level) listed on the resulting page.
But when I click any of the Sub Category (2nd Level), then I get a blank page, in place of the Posts links related to that sub category (2nd Level)
So here's where the second part of code comes into play
Now for the Second part of the code
------------------------------------------------------------------------------------------------------------------------------------
<?php $children = $wpdb->get_results( "SELECT term_id FROM $wpdb->term_taxonomy WHERE parent=$category_id" );
$no_children = count($children); ?>
<?php if ($no_children == "0") { ?>
<?php $recent = new WP_Query("cat=$this_category->cat_ID, &orderby=id&depth=0"); while($recent->have_posts()) : $recent->the_post();?>
<ul>
<li><b><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></b></li>
</ul>
<?php endwhile; ?>
<?php } ?>
------------------------------------------------------------------------------------------------------------------------------------
What this does is basically queries the wordpress database and counts the number of sub categories that any particular category might had. The "$no_children" is the number of children (Sub categories) of any main category.
Based on this, I compare the value of "$no_children" with "0".
If it's true then I want the recent posts from that subcategory to be displayed as links as signified by "<li><b><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></b></li>"
Now I'm expecting that when this loop will reach the 2nd level subcategory, It will fail to find any child and hence the list of posts in that sub category will be generated underneath.
Do correct me If I'm wrong somewhere in syntax or otherwise. (As I'm not very sure on this)
RESULT:
When I run this code, then I get the lists of recent posts at all levels (At every category page), When I only want that to be displayed after the final sub category level
Any suggestions,
Thanks in advance
Bhupinder
-
Dan Liptak -
Thanks
{{ DiscussionBoard.errors[1948006].message }} -
-
howudoin -
Thanks
{{ DiscussionBoard.errors[1948478].message }} -