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

Excluding First Parent Category or Designated Category WordPress

  • SOLVED

Hi,
I'm using the code below to display the category ID of my portfolio posts. The problem is, all of my posts have the parent category Portfolio. So when I output the category ID, they are all the same. So I want to skip over that category when listing the category ID.

If it helps, my portfolio category can be accessed with the following tag:


<?php echo get_option('freshstart_portfolio_cat'); ?>


Category code:


<?php
foreach((get_the_category()) as $category) {
echo $category->cat_ID;
}
?>

Answers (4)

2010-11-01

Nilesh shiragave answers:



<?php
$cat_id=get_option('freshstart_portfolio_cat');
foreach((get_the_category()) as $category) {

if($category->cat_ID==$cat_id)
{
continue;
}

echo $category->cat_ID;

}


Nilesh shiragave comments:



<?php
$cat_id=get_option('freshstart_portfolio_cat');
foreach((get_the_category()) as $category) {

if($category->catID==$cat_id)
{
continue;
}

echo $category->cat_ID;

}


Mike McAlister comments:

This is almost there! Just need one small mod. If there ARE no children categories of the portfolio category, it should just display the portfolio category id.


Nilesh shiragave comments:


Try this

<?php

$cat_id=get_option('freshstart_portfolio_cat');
$args=array(
'child_of' => $cat_id
);
$child_cat= get_categories( $args );
if(count($child_cat)>0)
{
foreach((get_the_category()) as $category) {

if($category->cat_ID==$cat_id)
{
continue;
}

echo $category->cat_ID;
}
}
else
{
echo $cat_id;
}
?>


Mike McAlister comments:

That one doesn't seem to be working, but it looks like it should. Any other ideas?


Nilesh shiragave comments:

Try this


<?php

$cat_id=get_option('freshstart_portfolio_cat');

$child_cat=get_categories(array('child_of'=>$cat_id));


if(count($child_cat)>0)
{
foreach($child_cat as $cats)
{
echo $cats->cat_ID.'<br />';
}
}
else
{
echo $cat_id;
}

?>


This code will display childs of the portfolio category whose category id is entered by using custom field.

And if there are no any child categories available then the parent category id will be displayed.

let me know if you want anything else.


Mike McAlister comments:

Something wrong with that one. This one is printing out multiple categories, and the same for each post.

I have increased the prize amount for the extra work this is taking.


Mike McAlister comments:

The answer you posted at 2:08am worked for the most part. It just wasn't displaying the parent category if there were no children.


Nilesh shiragave comments:

Try this code it will work


<?php
$cat_id=get_option('freshstart_portfolio_cat');
$all_cat=get_the_category();
if(count($all_cat)>0)
{
foreach($all_cat as $category) {
if($category->cat_ID==$cat_id)
{
continue;
}
echo $category->cat_ID;
}
}
else
{
echo $cat_id;
}
?>


Nilesh shiragave comments:

Try this code it will work


<?php
$cat_id=get_option('freshstart_portfolio_cat');
$all_cat=get_the_category();
if(count($all_cat)>0)
{
foreach($all_cat as $category) {
if($category->cat_ID==$cat_id)
{
continue;
}
echo $category->cat_ID;
}
}
else
{
echo $cat_id;
}
?>


Nilesh shiragave comments:

Try this sorry for the previous.


<?php
$cat_id=get_option('freshstart_portfolio_cat');
$all_cats=get_categories(array('child_of'=>$cat_id));
if(count($all_cats)>0)
{
$cat=get_the_category();
foreach($cat as $category) {
if($category->cat_ID==$cat_id)
{
continue;
}
echo $category->cat_ID;
}
}
else
{
echo $cat_id;
}
?>


Mike McAlister comments:

Still not working. See the graphic attached to see what I'm seeing.