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

display a specific category level of a post inside the loop WordPress

  • SOLVED

I'm trying to find a way to display a specific category level of a post inside the loop.

For example. I have a post with a hierarchical category tree structure like below...
<blockquote>
Parent cat

-Child cat A

--Child cat B

---Child cat C</blockquote>

I'm looking for a nifty piece code I can use (and edit according to the category level) within the loop that will allow me to display any single category level, not all the levels.

e.g. <?php display_cat_level (1) ; ?> will display ONLY "Parent cat" or <?php display_cat_level (3) ; ?> will display ONLY "Child cat B"

Answers (2)

2014-02-18

Sébastien | French WordpressDesigner answers:

write this code in functions.php

function display_cat_level($level){

$categories = get_the_category();
$tree = get_category_parents($categories[0]->term_id, FALSE, '||', TRUE);
$parent = explode('||',$tree);
echo $parent[$level-1];
}

use in the loop like that

//for example
display_cat_level(2);

My code is similar at the code of Fahd Murtaza, but instead of the code of Fahd Murtaza, my code display just the name of the category you want. Not the names of all parents.
I hope that is what you want.





and more handly :
if you use this function like that
display_cat_level(3);
it will display ONLY "Child cat B" like that :
Child cat B
but if you use this function like that
display_cat_level(3,true);
it will display ONLY "Child cat B" with link like that :
[[LINK href="http://your_site.com/category/parent/catA/catB"]]Child cat B[[/LINK]]

this is the function :

function display_cat_level($level,$link=false){

$categories = get_the_category();
$tree = get_category_parents($categories[0]->term_id, FALSE, '||', TRUE);
$parent = explode('||',$tree);
if($link==true) {
$category_id = get_cat_ID($parent[$level-1]);
echo '<a href="'.get_category_link($category_id).'">'.$parent[$level-1]."</a>";
} else {
echo $parent[$level-1];
}


}

Paste it in functions.php
and use in your loop like that :
display_cat_level(2,true);
or like that
display_cat_level(2);


pjeaje comments:

Very close yet it doesn't show the very last category???


pjeaje comments:

Very close yet it doesn't show the deepest category???


pjeaje comments:

It only displays the cat slug not the cat title


Sébastien | French WordpressDesigner comments:

each category can be displayed.
The main category with this code :
display_cat_level(1);

and if your tree is like that
<blockquote>Parent cat

-Child cat A

--Child cat B

---Child cat C</blockquote>
the Child cat C will be displayed by this code :
display_cat_level(4);



and i have edit my first response to add the link in the name of the category, if you need


pjeaje comments:

It still isn't showing the deepest cat??


pjeaje comments:

1. the slug is displaying not the title
2. the deepest cat (display_cat_level(4);) is not displaying


pjeaje comments:

@Fahd Murtaza - thanks title is now showing, but the deepest cat is still not showing.


Sébastien | French WordpressDesigner comments:

this is the good way :-)

function display_cat_level($level,$link=false){

$categories = get_the_category();
$nbr=count($categories)-1;
$tree = get_category_parents($categories[$nbr]->term_id, FALSE, '||', TRUE);
$parent = explode('||',$tree);
$category_id = get_cat_ID($parent[$level-1]);
$title = get_category_by_slug($parent[$level-1]);
$title = $title->name;

// if($level == count($tree))

if($link==true) {
echo '<a href="'.get_category_link($category_id).'">'.$title."</a>";
} else {
echo $title;
}
}


pjeaje comments:

Sorry, nothing displays?


Sébastien | French WordpressDesigner comments:

try again, all is ok on my site...
Have you write display_cat_level(3); in your loop ?


pjeaje comments:

Nope nothing


Sébastien | French WordpressDesigner comments:

if you want, you could send me your id to wp backoffice and I look at that.



Sébastien | French WordpressDesigner comments:

is it ok now ? All is ok on my dev server


pjeaje comments:

No, your code displays nothing.


Sébastien | French WordpressDesigner comments:

Could you send me your id to connect backoffice ?


Sébastien | French WordpressDesigner comments:

I have made a screencast. You can see on this video that all is ok.
So, if you want I can look at your backoffice.
My code works perfectly.
[[LINK href="http://screencast-o-matic.com/watch/c2nFI7nIKa"]]http://screencast-o-matic.com/watch/c2nFI7nIKa[[/LINK]]


pjeaje comments:

email sent


pjeaje comments:

Thanks!!

function display_cat_level($level,$link=false){

$categories = get_the_category();
//$categories = array_reverse($categories);


$nbr=count($categories)-2;
//print_r($categories[$nbr]->term_id);
$tree = get_category_parents($categories[$nbr]->term_id, FALSE, '||', TRUE);
$parent = explode('||',$tree);


$title = get_category_by_slug($parent[$level-1]);
$title = $title->name;
$category_id = get_cat_ID($title);
if($link==true) {
echo '<a href="'.get_category_link($category_id).'">'.$title."</a>";
} else {
echo $title;
}
}


tag: <?php display_cat_level(<em><strong>2</strong></em>,true); ?>


pjeaje comments:

Works 100%!!!



pjeaje comments:

Correction - the code doesn't work. It only works if there is only 1 child category per category. If there is 2 or more children then the code breaks.

2014-02-18

Fahad Murtaza answers:

Try this


function display_cat_level($level){
$categories = get_the_category();
$tree = get_category_parents($categories[0]->term_id, FALSE, ':', TRUE);
$parent = explode(':',$tree);
if(count($parent)>$level){
$slug=$parent[$level];
$idObj = get_category_by_slug($slug);
$category_link = get_category_link( $idObj->term_id );
echo "<a href='".$category_link."'>".$idObj->name ."</a>";
}

}


Fahad Murtaza comments:

Actually, you are right, this could be a simple function which limits the recursive calls to

<?php echo get_category_parents( $cat, true, ' &raquo; ' ); ?>


Fahad Murtaza comments:

I am testing my code right now. Lets see if I can code this quick.


pjeaje comments:

That tells me nothing... unless you'd like to tell me what to do with it?


Fahad Murtaza comments:



function display_cat_level($level){
$categories = get_the_category();
$tree = get_category_parents($categories[0]->term_id, FALSE, ':', TRUE);
//echo $tree;
$parent = explode(':',$tree);
$mybodyclass=$parent[0];
//echo $mybodyclass;
for($count=0;$count < $level;$count++){
echo $parent[$count];

}

}


Fahad Murtaza comments:

More refined code


function display_cat_level($level){
$categories = get_the_category();
$tree = get_category_parents($categories[0]->term_id, FALSE, ':', TRUE);
$parent = explode(':',$tree);
for($count=0;$count <= $level;$count++){
echo $parent[$count];

}


Fahad Murtaza comments:

Put that function in functions.php and use it anywhere in a post loop.


pjeaje comments:

So how do I edit the code to choose which category level to display?


pjeaje comments:

the function crashes my website


Fahad Murtaza comments:

My bad. It was missing the ending bracket



function display_cat_level($level){
$categories = get_the_category();
$tree = get_category_parents($categories[0]->term_id, FALSE, ':', TRUE);
$parent = explode(':',$tree);
for($count=0;$count <= $level;$count++){
echo $parent[$count];

}

}


You can call it like you described in the question i.e

<?php display_cat_level (1) ; ?>


Fahad Murtaza comments:

Above code will display all categories according to level. If you want to list just one category based on the number of level, you can do so with this mod to my original code

function display_cat_level($level){
$categories = get_the_category();
$tree = get_category_parents($categories[0]->term_id, FALSE, ':', TRUE);
$parent = explode(':',$tree);
echo $parent[$level-1];

}


The reason I used $level -1 is the way PHP creates its array. It starts with 0th index not 1. So if you want the 2nd level, you call the dunction like this

<?php display_cat_level (2) ; ?>

and it shows the parent category at level 1.


Fahad Murtaza comments:


function display_cat_level($level){
$categories = get_the_category();
$tree = get_category_parents($categories[0]->term_id, FALSE, ':', TRUE);
$parent = explode(':',$tree);
if(count($parent)>$level-1){
$slug=$parent[$level-1];
$idObj = get_category_by_slug($slug);
echo $idObj->name;
}

}


pjeaje comments:

@Fahd Murtaza - thanks title is now showing, but the deepest cat is still not showing.


Fahad Murtaza comments:

Please explain what you mean by deepest cat.

Parent cat

-Child cat A

--Child cat B

---Child cat C


<strong>Child cat C </strong>? or <strong>Parent cat</strong>


pjeaje comments:

---Child cat C


pjeaje comments:

http://snag.gy/IhWrJ.jpg


pjeaje comments:

It isn't displaying "Swimming"


pjeaje comments:

Can you make it like Sébastien with the option to link the title?


Fahad Murtaza comments:

OK, all done


function display_cat_level($level){
echo $level;
$categories = get_the_category();
$tree = get_category_parents($categories[0]->term_id, FALSE, ':', TRUE);
echo $tree."<br/>";
$parent = explode(':',$tree);
if(count($parent)>$level){
$slug=$parent[$level];
$idObj = get_category_by_slug($slug);
$category_link = get_category_link( $idObj->term_id );
//print_r( $idObj);
echo "<a href='".$category_link."'>".$idObj->name ."</a>";
}

}


pjeaje comments:

Not quite...!


Fahad Murtaza comments:

Clean code

Try this


function display_cat_level($level){
$categories = get_the_category();
$tree = get_category_parents($categories[0]->term_id, FALSE, ':', TRUE);
$parent = explode(':',$tree);
if(count($parent)>$level){
$slug=$parent[$level];
$idObj = get_category_by_slug($slug);
$category_link = get_category_link( $idObj->term_id );
echo "<a href='".$category_link."'>".$idObj->name ."</a>";
}

}


pjeaje comments:

Not displaying a thing


Fahad Murtaza comments:

Works fine for me. Can you try one thing. Just select the deepest category when selecting post categories. See if it works for you.


pjeaje comments:

That would be pointless as I NEED to check all the cats


pjeaje comments:

Doesn't make a difference


pjeaje comments:

Sorry the deepest cat shows now but i need to check all cats


pjeaje comments:

When I check all the cats, the deepest cat doesn't show?


Fahad Murtaza comments:

yes, apparently this function I used doesn't play nice when multiple categories are selected in the same tree.


pjeaje comments:

So close yet so far!


pjeaje comments:

Your code displays nothing


pjeaje comments:

Sorry wrong person!


Fahad Murtaza comments:

OK

It works just fine for me. Not sure whats wrong on your side. Anyhow, good luck!


Fahad Murtaza comments:

Haha :)


pjeaje comments:

Let's try this... do you have a piece of code that will display ONLY the deepest cat?