Wordpress Themes - WP Forum at BFA
Click Here To DONATE! Support the theme development, get more attention and additional benefits based on your donation level. Your forum name will be highlighted.

Wordpress Themes - WP Forum at BFA » WordPress Themes » Atahualpa 3 Wordpress theme » Center area post/pages » Post-Kicker, -Byline & -Footer »

Trying to display Parent Category as header for Sub-Categories


Build custom WP THEMES, without
coding.
Click here for THEMEFRAME

From the author of Atahualpa, the #1 most
downloaded (700,000+) theme @ wordpress.org
  #1  
Old Aug 19, 2010, 11:59 PM
rodrigo
 
4 posts · Aug 2010
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!
  #2  
Old Aug 21, 2010, 06:15 PM
rodrigo
 
4 posts · Aug 2010
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?
  #3  
Old Aug 24, 2010, 01:19 AM
rodrigo
 
4 posts · Aug 2010
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.
  #4  
Old Aug 24, 2010, 06:55 AM
juggledad's Avatar
juggledad
 
22,253 posts · Mar 2009
OSX 10.8.5 WP 3.8.x Atahualpa 3.7.x Safari 6.x Firefox 25 XP
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)
__________________
"Tell me and I forget, teach me and I may remember, involve me and I learn." - Benjamin Franklin
Juggledad | Forum Moderator/Support
  #5  
Old Aug 26, 2010, 10:47 AM
rodrigo
 
4 posts · Aug 2010
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...

Bookmarks

Tags
categories, category, footer links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Display posts alphabetically for one category, chronologically for other categories spencer Atahualpa 3 Wordpress theme 4 Jul 16, 2014 09:01 AM
How to display posts from one parent category only on 'results' page JakeThePeg Center area post/pages 2 Sep 20, 2010 08:44 PM
Parent Categories Won't Produce Children Sonjapearl Page & Category Menu Bars 3 Feb 27, 2010 03:23 PM
Can I make pages only display main categories and not sub categories buddha Page & Category Menu Bars 0 Oct 24, 2009 06:53 PM
disable links in category menu for parent categories (only childless categories link) qcook2000 Page & Category Menu Bars 0 May 15, 2009 09:09 PM


All times are GMT -6. The time now is 06:44 AM.


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