Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Retreiving Child Post Categories. WordPress

  • SOLVED

Hi there,

I have a custom option in the wordpress dashboard that allows the user to select the parent category of a particular group of categories.

When I retrieve this value it comes back as the 'name' of the category. (IE the value of the users category selection is "Portfolio")

I need to know how to retrieve the children of the "Portfolio" category and store those values in a variable so that I can display only posts that are children of the "Portfolio" category.

Your assistance is greatly appreciated.

Cheers.

Answers (2)

2011-06-21

Nilesh shiragave answers:

You can retrieve category id using category name by using following code


function get_category_id($cat_name){
$term = get_term_by('name', $cat_name, 'category');
return $term->term_id;
}
$category_ID = get_category_id('Portfolio');


And all the child categories by using


$args=array(
'child_of' => $category_ID
);
$categories=get_categories($args);


for more details check

http://codex.wordpress.org/Function_Reference/get_categories


WP Answers comments:

Thank you very much for this. It is working nicely. :)

I have one more question related to this. (I can increase prize money if you need)

My other request is to generate an unordered list of only the child categories. Each item will also need a "data-id" with the category name.

Here is an example of the output I will need:

<ul class="category_filter_list">
<li data-value="red"><a href="#">Red</a></li>
<li data-value="blue"><a href="#">Blue</a></li>
<li data-value="yellow"><a href="#">Yellow</a></li>
</ul>

I greatly appreciate your assistance

Cheers


Nilesh shiragave comments:

User this code after my previous code


<ul class="category_filter_list">
<?php
foreach ($categories as $category) {
echo '<li data-value="'.$category->cat_name.'"><a href="#">'.$category->cat_name.'</a></li>';
}
?>
</ul>


WP Answers comments:

Thank you very much for the additional code, however it seems to be displaying every post category instead of only the child categories.

Here is the full code below. Can you please let me know if I've made an error in the code?


function get_category_id($cat_name){
$term = get_term_by('name', $cat_name, 'category');
return $term->term_id;
}

$category_ID = get_category_id($portfolio_parent);



$args=array(
'child_of' => $category_ID
);

$categories=get_categories($args);



echo '<ul class="category_filter_list">';
$category_id=get_categories($args);
foreach ($categories as $category) {
echo '<li data-value="'.$category->cat_name.'"><a href="#">'.$category->cat_name.'</a></li>';
}
echo '</ul>';


Nilesh shiragave comments:

Add this code in functions.php


function get_category_id($cat_name){

$term = get_term_by('name', $cat_name, 'category');

return $term->term_id;

}


And following code where you want to display this child categories


$category_ID = get_category_id($portfolio_parent);
$args=array(
'child_of' => $category_ID
);
$categories=get_categories($args);

echo '<ul class="category_filter_list">';

foreach ($categories as $category) {

echo '<li data-value="'.$category->cat_name.'"><a href="#">'.$category->cat_name.'</a></li>';

}


How you are setting $portfolio_parent value?


WP Answers comments:

Hi There,

Thank you again, however when I make those coding changes, none of the page content gets displayed at all. This is generally caused by error in the code correct?

The $portfolio_parent is set from a custom meta box. On the page editing screen there is a dropdown list which displays all post categories. The user selects the category and that category is stored in $portfolio_parent


Nilesh shiragave comments:

Add this code in functions.php

function get_category_id($cat_name){

$term = get_term_by('name', $cat_name, 'category');

return $term->term_id;

}


And following code where you want to display this child categories







<?php
$category_ID = get_category_id($portfolio_parent);
$args=array(

'child_of' => $category_ID

);

$categories=get_categories($args);
echo '<ul class="category_filter_list">';
foreach ($categories as $category) {
echo '<li data-value="'.$category->cat_name.'"><a href="#">'.$category->cat_name.'</a></li>';
}
echo '</ul>';
?>


Can you check in source code what is error. you can view that error in source code.?


WP Answers comments:

Sure thing. I turned on WP_DEBUG and got the following error:

Fatal error: Cannot redeclare get_category_id() (previously declared in /Users/macuser/Dropbox/MAMP/theme-demo/wp-content/themes/New-Theme/functions.php:51) in /Users/macuser/Dropbox/MAMP/theme-demo/wp-content/themes/New-Theme/functions/content/portfolio-content-4-col.php on line 12


Nilesh shiragave comments:

can you remove



function get_category_id($cat_name){
$term = get_term_by('name', $cat_name, 'category');
return $term->term_id;
}



this code from content-4-col.php i think it is on line number 12


WP Answers comments:

Hi There,

I got it working properly :)

I am using your original solution for displaying the unordered list.

it seems that I needed to re-define the $portfolio_parent value within the portfolio page rather than in an external file that is called in globally.

Thank you very much for all of your help.

Cheers.


The working code is:

echo '<ul class="category_filter_list">';
$category_id=get_categories($args);
foreach ($categories as $category) {
echo '<li data-value="'.$category->cat_name.'"><a href="#">'.$category->cat_name.'</a></li>';
}
echo '</ul>';


WP Answers comments:

Hi,

WPQuestions is not giving me the option to select a winning answer. Do you know how I can send you the prize money?


Nilesh shiragave comments:

Good.... finally that worked.... i hope you will increase price money :)


WP Answers comments:

I "voted" on this question and selected you as the winner. Please let me know if you received the prize money.

I think there is bug with WPQuestions site because it normally displays a link to "Select as winner".

I was also unable to increase prize money but I am glad to send you more prize money via PayPal. Please let me know your PayPal ID and I will send over more prize.

Cheers

2011-06-21

John Cotton answers:

This kind of thing will do it....


$cat = get_category_by_slug('portfolio');
$categories = get_categories( array( 'child_of' => $cat->term_id) );


PS you might want to check the default args for [[LINK href="http://codex.wordpress.org/Function_Reference/get_categories"]]get_categories[[/LINK]]


WP Answers comments:

Thank you for this code, however the parent category might not always be named "portfolio". The user may have a different name.

I tried replacing ('portfolio'); with the variable that stores the value of the users selection ($port_value); but it doesn't seem to be properly displaying the categories.

Is there a different syntax I will need to use?

Thank you much


John Cotton comments:

Try this then:


$cat_id= get_category_id($port_value);
$categories = get_categories( array( 'child_of' => $cat_id) );