tronicscribe
Member
Posted 7 months ago #
I made a category.php file, and I have the following code in it.
<?php
if (is_category('1'))
{include "cat_showcase.php";}
if (is_category('36'))
{include "cat_blog.php";}
?>
This works perfectly for cats 1 and 36. However I would like to also say something like if the category is a child of category 1 then include cat_showcase.php. Because I have several sub cats, and cat 1 is the parent. Right now if I click on a sub cat it doesn't load anything. Is there a way to tell it to look at all children of a parent category so I don't have to have an array listing every single sub category I have? Thanks!
if ( is_category() ) {
$cat = get_query_var('cat');
$child = get_category($cat);
echo 'parent is ' . $child->category_parent;
}
tronicscribe
Member
Posted 7 months ago #
Thank you MichaelH, but would you mid explaining what this does to make it work, and how I would need to change it to work with my template files? I only understand some basic php and I'm not too sure how some of those wp functions work. Such as, would I need to specify the parent cat somewhere in that statement, and where would I tell it what template.php file to load? Thanks again for supplying the code, and I hope you don't mind explaining it a bit.
if a category query
$cat is the category being queried
get the category information for $cat
echo the parent category for $cat (you would leave this line out)
so you would test something like
<?php
if ( is_category() ) {
$cat = get_query_var('cat');
$child = get_category($cat);
if ( 1 == $child->category_parent ) {
//do your include thing
}
}
?>
tronicscribe
Member
Posted 6 months ago #
thanks for the explanation. although I'm still not able to get it to work. in your code I changed == to != and it works with category 1. but it is still not detecting all the child categories. I'm trying to play around with it, but I don't know how far I will actually get. if you find the time or desire to help me out any further, I'd appreciate it. Otherwise, thanks so much for all your help this far, I know I'm on the right track.
This worked just fine for me in category.php using the WordPress Default theme as long as category 1 is the PARENT of the current category being queried.
<?php
if ( is_category() ) {
$cat = get_query_var('cat');
$child = get_category($cat);
if ( 1 == $child->category_parent ) {
include(TEMPLATEPATH . "/cat1include.php");
}
}
?>
tronicscribe
Member
Posted 6 months ago #
as long as category 1 is the PARENT of the current category being queried.
Oh I'm so glad you said that. It made me realize I needed to first define the template for parent category 1 before I used your code to get the children. So I added this before your code.
if (is_category('1'))
{include "cat_showcase.php";}
Before I did that, I was trying to view category 1 before I tried to view the child categories and it wasn't showing the page. Now I can view cat 1 before choosing a child cat.
Thanks for all your help, and I hope I don't run into any other roadblocks.
Or another 'version'
<?php
// if a category view see if category is id=1 or 'direct child' of 1
if ( is_category() ) {
$cat = get_query_var('cat');
$args = array(
'include' => $cat,
'hide_empty' => 0
);
$categories = get_categories($args);
if ( ($cat == 1) || ($categories[0]->category_parent == 1) ) {
echo 'you are viewing cat 1 or one of the DIRECT child';
}
}
?>
emanoelmelo
Member
Posted 5 months ago #
What if we where talking about a single post? Like if a post was in some category or that category's child? I've tried to use in_category instead of is_category but no results...