Ask your WordPress questions! Pay money and get answers fast! (more info)

Current, sibling and parent category on single sidebar

  • SOLVED

Hi all,

I need a snippet of code to display in the sidebar of single.php, the top level category that the post resides in and all its subcategories only.

Answers (3)

2012-07-24

Hai Bui answers:

Try this:


<?php
global $post;
$cats = get_the_category($post->ID);
$catid = $cats[0]->cat_ID;
while ($catid) {
$cat = get_category($catid);
$catid = $cat->category_parent;
$catParent = $cat->cat_ID;
}

$categories = get_categories('child_of='.$catParent);
foreach ($categories as $category) {
echo '<a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name.'</a>';
}
?>


Hai Bui comments:

Try this instead. I add a few lines to print parent category.
<?php
global $post;
$cats = get_the_category($post->ID);
$catid = $cats[0]->cat_ID;
while ($catid) {
$cat = get_category($catid);
$catid = $cat->category_parent;
$catParent = $cat->cat_ID;
}

$catParentObj = get_category($catParent);
echo '<a href="'.get_category_link($catParent).'">'.$catParentObj->cat_name.'</a>';

$categories = get_categories('child_of='.$catParent);
foreach ($categories as $category) {
echo '<a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name.'</a>';
}
?>


xhanubis comments:

Hey Dude,

That nailed it thanks. Thanks to all the other guys who also chipped in their 2 cents!

2012-07-24

Martin Pham answers:

try this

function get_child_cat_single() {
global $post;
$parent_cat = get_the_category($post->ID);
$cat_id = $parent_cat[0]->category_parent;
$categories = get_categories('child_of='.$cat_id);
foreach ($categories as $category) {
echo $category->cat_name;
// do stuff
}
}


Martin Pham comments:

Codex: [[LINK href="http://codex.wordpress.org/Function_Reference/get_categories"]]http://codex.wordpress.org/Function_Reference/get_categories[[/LINK]]


xhanubis comments:

Didn't work sorry.

2012-07-24

Arnav Joy answers:

try this
<?php
global $wpdb;
$query = "SELECT tt.term_id FROM ".$wpdb->prefix."term_relationships tr LEFT JOIN ".$wpdb->prefix."term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id =". get_the_ID()." AND tt.taxonomy='category' ORDER BY tt.term_id DESC";
$rows = $wpdb->get_results($query);
if($rows){
foreach($rows as $row){
$termID[] = $row->term_id;
}
}

$topParentCatID = $termID[0]; // id of the top parent category

$categories = get_categories( array('child_of' => $topParentCatID , 'hide_empty' => 0) );
foreach ($categories as $category) {
$childCategriesID[] = $category->term_id;
}

print_r($childCategriesID);
?>


xhanubis comments:

Didn't work...I however found a bit of code that returned all the subcategories from the current posts top level categories. Problem though is that:

A. it does not return the categories as clickable links
B. It does not return the parent category just the category of the current post and its siblings.


<ul>
<?php
$post_categories = wp_get_post_categories( $post->ID );
foreach($post_categories as $c){
$cat = get_category( $c );

$childCats = get_categories( array('child_of' => $c) );
if(is_array($childCats)):
foreach($childCats as $child){ ?>
<li><?php echo $child->name; ?></li>
<?php
query_posts('cat='.$child->term_id);
while(have_posts()): the_post(); $do_not_duplicate = $post->ID;
//post code stuff here;
endwhile;
wp_reset_query();
}
endif;
}

?></ul>


Arnav Joy comments:

find following

<li><?php echo $child->name; ?></li>

and replace with



<li><a href="<?php echo get_category_link($child->term_id); ?>"><?php echo $child->name; ?></a></li>


Arnav Joy comments:

my above solution will solve your first problem

<blockquote>A. it does not return the categories as clickable links</blockquote>


Arnav Joy comments:

Try this


<ul>
<?php
$post_categories = wp_get_post_categories(get_the_ID());

$parentCatID = get_top_parent($post_categories[0]);

echo '<li><a href="'.get_category_link($parentCatID).'">'.get_cat_name($parentCatID).'</a></li>';

$childCats = get_categories( array('child_of' => $parentCatID , 'hide_empty' => 0) );

if(is_array($childCats)){
foreach($childCats as $child){
echo '<li><a href="'.get_category_link($child->term_id).'">'.get_cat_name($child->term_id).'</a></li>';
}
}


?>
</ul>



and define paste this function in functions.php

<?php
function get_top_parent($cat){
$curr_cat = get_category_parents($cat, false, '/' ,true);
$curr_cat = explode('/',$curr_cat);
$idObj = get_category_by_slug($curr_cat[0]);
return $id = $idObj->term_id;
}
?>