Need Help With php IF statement (WordPress Related)

2 replies
Let me say beforehand that this is my first attempt at implementing a PHP code although I'm acquainted with wordpress.

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
#if statement #php #programming error #related #statement #wordpress
  • Profile picture of the author Dan Liptak
    In the second part of the code, the WHERE clause, WHERE parent=$category_id, I don't see where you are setting $category_id. Maybe it should be $this_category->cat_ID?

    Later,
    Dan
    {{ DiscussionBoard.errors[1948006].message }}
  • Profile picture of the author howudoin
    In the second part of the code, the WHERE clause, WHERE parent=$category_id, I don't see where you are setting $category_id. Maybe it should be $this_category->cat_ID?

    Later,
    Dan
    Thanks for the help Dan.

    Although I figured this out on my own, your answer is 100% correct. That was the tiny part which was causing all the trouble!

    Now I've run into other problem:

    Is it possible In wordpress to assign same Sub Category to two different categories. From all the digging I've done so far, it seems this is not possible without some hack.

    So do you know some hack or other solution to this?

    Thanks in advance
    Bhupinder
    {{ DiscussionBoard.errors[1948478].message }}

Trending Topics