Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKonrad Pilch
2,435 PointsCustom Types Categories
I have articles from Custome Post Types, and so far i got everything i need, apart from categories. I have slected to have taxonomies categories, but i need to have each category a different text color, and i need to loop throw them on my page to show off. I have looked on google but i canβt find a damn thing about it, at least to my understanding, or either im looking wrong.
Konrad Pilch
2,435 PointsOkay, mayhbe now.. the link i used is here and the actuall code is
<?php
$customPostTaxonomies = get_object_taxonomies('article');
if(count($customPostTaxonomies) > 0)
{
foreach($customPostTaxonomies as $tax)
{
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
'title_li' => ''
);
wp_list_categories( $args );
}
}
?>
And now you just need to change the
get_object_taxonomies('changeheretoYourNameOfwhatYouNamedIt');
The args passing is i beleive, you are ordering it by names, it counts alpabetical order, i suppose, show_count to 0? i thik thats the default to show all posts, pad_count? no idea, nor the last ones after it.
wp_list_categories is a default function of WordPress, and we are just passing the args in. It's like WP_Query, at least form my point of view.
Hopefully I'm correct, im trying to learn as much as I can, and because I don't have a blog yet, I'm going to write it here lol plus if someone googles it he might find it :)
Konrad Pilch
2,435 PointsThe count shows how many posts are in the category
Konrad Pilch
2,435 Points <p class="blog-date"><?php the_time('F j, Y'); ?> in
<span class="category"><a href="">
<?php
$category = get_the_category( $post->ID );
echo $category[0]->cat_name;?>
</span>
</p>
This code, inside the loop, will show the category assigned to the post.
So, we create a variable $category, and we use the wp default function which is
get_the_category();
now, i think $post-ID, is getting the ID of the current post, because I'm using the W_Query to get the posts, each post has it's own id, so it's already knows whcih post it is, so we are now basically grabbing an id of the post
now we are echo, so displaying the above code which we saved in variable $category, and the cat_name , as the description tells me
Retrieve the ID of a category from its name.
And i have no idea what is the [0], some array or no idea, if anonyne knows please tell me.
2 Answers
Konrad Pilch
2,435 PointsJust because I solved most of the parts :) and probably will solve the last one, unless someone comes and help me.
Joel Bardsley
31,249 PointsRegarding the [0], as posts/pages can have multiple categories in WordPress, the get_the_category() function returns an array of objects - one for each category assigned to the current post ID (or another post ID, if passed as a parameter). So echo $category[0]->cat_name; is just retrieving the name of the first category in the returned array.
Konrad Pilch
2,435 PointsSO if we have HTML and CSS as category, and we select one, with the [0] we are telling to get the selected category in form of get the first on category thats picked, so basically if we pick CSS, it will go and look for the ticked category right?
Konrad Pilch
2,435 PointsBtw, do you know how can i make each category to have a different text color?
Joel Bardsley
31,249 PointsIf the post has just the CSS category ticked, it will pick that one. If it has CSS and HTML ticked, I think the array will have them sorted in alphabetical order, so $category[0] would still refer to CSS. If none are ticked, I expect it will fallback to the 'uncategorized' category.
Konrad Pilch
2,435 PointsKK thank you :)
Joel Bardsley
31,249 PointsFor the styling each category, when looping through the posts use the post_class() function in your template, which will generate all the classes you'll need to apply css for each category.
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 PointsOkay..... that was easy... but where is this code in documentation? Please help me on how to use documentation : d the code to loop them throw the articles page is
<?php wp_list_categories( $args ); ?>
but thats just one page, what if 2,3? I believe i pass the argument of articles paage like custom types doe.. im sure..
but what about having each of them different color?