Wordpress Themes - WP Forum at BFA

Wordpress Themes - WP Forum at BFA (http://forum.bytesforall.com/index.php)
-   Post-Kicker, -Byline & -Footer (http://forum.bytesforall.com/forumdisplay.php?f=17)
-   -   Trying to display Parent Category as header for Sub-Categories (http://forum.bytesforall.com/showthread.php?t=9059)

rodrigo Aug 19, 2010 11:59 PM

Trying to display Parent Category as header for Sub-Categories
 
Hello --

I recently dove headfirst into the world of Wordpress, Atahualpa, CSS and PHP, so I hope my question here makes sense. I have been trying to accomplish as much as I can within the parameters of the Atahualpa theme, and I am hoping this won't be too difficult.

I have set up my list of categories, and I also have many sub-categories (children). So, using the common example I have seen on other threads, I have:

Fruits
---apples
---oranges
---bananas
---strawberries

Vegetables
---carrots
---celery
---onions
---turnips

For each post, I assign the relevant children categories. For example, Post X will be assigned Apples, Bananas, Carrots, and Onions.

When I display a post on a page, I would like to display each relevant Parent Category first, followed by a semi-colon, and then the relevant child categories. In other words, I would like to display the following in the footer for the example above:

Fruits: Apples, Bananas
Vegetables: Carrots, Onions


Right now, it just displays:

Categories: Apples, Bananas, Carrots, Onions.

So, I guess I need to set up some PHP script that says something like, for each category parent, if child categories are present for that parent, then print "Parent Name", followed by a semi-colon, followed by all relevant Child Categories.

I think this seems possible with some of the category functions, I just don't know enough about all this and can't seem to find anything in the threads.

Thanks for any help!

rodrigo Aug 21, 2010 06:15 PM

I decided to try and look over some various WP and PHP functions, to see if I could make an attempt at coding what I was trying to explain above. So, in case this might give someone a better idea as to what I am trying to do, here is my attempt at roughing out what I think might need to be there. I am sure there is a far more elegant solution, and the following code is most definitely pseudo-code, so don't try pasting this into your site.

<?php

$fruit_cats = array()
$vegetable_cats = array()

foreach ((get_the_categories() ) as $category ) {

$parent = get_category_parents($category);

if $parent = "fruits" {
array_push($fruit_cats, $category);
}

elseif $parent = "vegetables" {
array_push($vegetable_cats, $category);
}

else {
/* do nothing */
}
}

if $fruit_cats != "" {
echo "fruits: " $fruit_cats;
}


if $vegetable_cats != "" {
echo "vegetables: " $vegetable_cats;
}

?>




Does something like that make any sense?

rodrigo Aug 24, 2010 01:19 AM

Well, in the absence of any input, I have blindly plowed ahead. Right now, I actually have something working that is pretty much doing what I want it to (with a few flaws). I am pretty stoked that I have gotten this far, though it has taken a LOT of trial and error.

I am using my actual variable names now, instead of the Fruits/Veg analogy. I figure I will just post where I am at so far in case anyone is playing along, or has any input. The code pasted below is all happening inside the loop -- I will add some questions/problems down below.

----------

<?php

$discCats = array();
$techCats = array();
$matCats = array();


foreach((get_the_category()) as $category) {

if ($category->category_parent == 29) {

array_push($discCats,$category->cat_name);
}

elseif ($category->category_parent == 23) {

array_push($techCats,$category->cat_name);
}

elseif ($category->category_parent == 13) {

array_push($matCats,$category->cat_name);
}

else {
//DO NOTHING
}
}


?> <BR /> <?php
echo "Discipline: ";

foreach ($discCats as $value) {

echo $value . " ";
}

?> <BR /> <?php

echo "Techniques: ";

foreach ($techCats as $value) {

echo $value . " ";
}

?> <BR /> <?php

echo "Materials: ";
foreach ($matCats as $value) {

echo $value . " ";
}

?> <BR /> <?php

?>


------------------------------------

So, here are a few questions / problems thus far.

- First, I am assuming there may be a better place for this chunk of code. Could this whole thing be wrapped up into a function or a separate file and called from various locations?

- Right now, the foreach loop is returning a list of category names. I want to add commas between them, but I am not certain how to avoid a comma after the last item without some giant counter loop outside all of this.

- Also, right now, while this gives me the Parent as a header/title before the list of categories, the categories listed when this is run are not links right now. I am not certain what I need to add to keep them as links.

juggledad Aug 24, 2010 06:55 AM

You could put this in a template and then add a page pointing at this template.

As for the comma issue, you could have a counter and just look to see if it is the first occurrence. If it is, just output the category, if it is not the first time, put out a comma, a blank and then the category

If you want them as links then you need to output HTML for a link ( the <a.....> syntax)

rodrigo Aug 26, 2010 10:47 AM

Thanks Juggledad! For some reason, I didn't think to put the comma and space first, then the category. I was thinking I would need to figure out which category was last and not put the comma there! Of course, this way is far easier and I just got it to work perfectly with a counter. I guess you just need some fresh eyes sometimes!

Now, on to revise the code and work on the link and templates...


All times are GMT -6. The time now is 06:34 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.