Wordpress Themes - WP Forum at BFA
There will be no more development for Atahualpa (or any other theme), and no support. Also no new registrations. I turned off the donation system. I may turn the forum to read only if it gets abused for spam. Unfortunately I have no time for the forum or the themes. Thanks a lot to the people who helped in all these years, especially Larry and of course: Paul. Take care and stay healthy -- Flynn, Atahualpa developer, Sep 2021

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


  #1  
Old Aug 20, 2010, 12:59 AM
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, 07: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, 02: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, 07:55 AM
juggledad's Avatar
juggledad
 
23,765 posts · Mar 2009
OSX 10.11.5 WP 4.x Atahualpa(all) Safari, Firefox, Chrome
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, 11: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



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 10:01 AM
How to display posts from one parent category only on 'results' page JakeThePeg Center area post/pages 2 Sep 20, 2010 09:44 PM
Parent Categories Won't Produce Children Sonjapearl Page & Category Menu Bars 3 Feb 27, 2010 04:23 PM
Can I make pages only display main categories and not sub categories buddha Page & Category Menu Bars 0 Oct 24, 2009 07:53 PM
disable links in category menu for parent categories (only childless categories link) qcook2000 Page & Category Menu Bars 0 May 15, 2009 10:09 PM


All times are GMT -6. The time now is 11:53 AM.


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