Try this:
<?php
// if a category view see if category is id=6 or 'direct child' of 6
if ( is_category() ) {
$cat = get_query_var('cat');
$args = array(
'include' => $cat,
'hide_empty' => 0
);
$categories = get_categories($args);
if ( ($cat == 6) || ($categories[0]->category_parent == 6) ) {
echo 'you are viewing cat 6 or one of the DIRECT child';
}
}
?>
Thread Starter
hilj
(@hilj)
This almost solved my problem. When it’s alone it works, but when I put two of them in a row the second one wont work. Like this:
<?php // The first works works.
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) ) {
wp_list_categories('child_of=1&orderby=name&order=DESC&title_li=');
}
} elseif // The second wont work.
( is_category() ) {
$cat = get_query_var('cat');
$args = array(
'include' => $cat,
'hide_empty' => 0
);
$categories = get_categories($args);
if ( ($cat == 18) || ($categories[0]->category_parent == 18) ) {
wp_list_categories('child_of=18&orderby=name&order=DESC&title_li=');
} ?>
Or in_category or is_category wont work after it.
Thanks Michael! This was so far very helpful!
You wouldn’t really need that 2nd set of code–
<?php
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) ) {
$child_of='child_of=1';
} elseif ( ($cat == 18) || ($categories[0]->category_parent == 18) ) {
$child_of='child_of=18';
} else {
$child_of='';
}
if ($child_of) {
wp_list_categories($child_of .'&orderby=name&order=DESC&title_li=');
}
}
?>
Thread Starter
hilj
(@hilj)
Thanks again Michael! That solved it for me π
Thread Starter
hilj
(@hilj)
One small issue arise while testing this.
The following doesn’t work in single.php file but works in archive and category templates.
If this post is in subcategory x get the sidebar-x.php.
<?php
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) ) {
include(TEMPLATEPATH . '/sidebar.php');
} elseif ( ($cat == 41) || ($categories[0]->category_parent == 41) ) {
include(TEMPLATEPATH . '/sidebar02.php');
} elseif ( ($cat == 18) || ($categories[0]->category_parent == 18) ) {
include(TEMPLATEPATH . '/sidebar03.php');
} else {
get_sidebar();
}
}
?>
Is there a way around this?
Thanks again! It’s been so helpful so far!
You’ll want to use the is_single() conditional. Also you need to get the categories (note plural) from the post itself…this example uses the FIRST category found on the post
if ( is_single() ) {
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$cat = $cats[0];
}
$args = array(
'include' => $cat,
'hide_empty' => 0
);
}
Just to clarify (there was some confusion in another thread about this) β you have to actually copy the definition of the post_is_in_descendant_category function mentioned in the Codex before you use it β it doesn’t come with WordPress. π