Marty Walker Posted October 4, 2012 Share Posted October 4, 2012 Hi, I've nearly got my coded worked out in setting up a basic blog-style categories system. I'm stuck on how to pull a list of categories into my sidebar. My category Page tree is setup thusly: - Categories -- Notes --- Design --- Development --- etc This is what I have so far: <?php $cat = $pages->get("/notes/")->find("template=notes, sort=-sort, publish_date<$today"); foreach ($cat as $category) { echo "<p><a href='{$page->path}categories/{$category->note_category->name}'>{$category->note_category->title}</a></p>"; } ?> ... which pulls the category names and links correctly. But if I've assigned a category to multiple articles (which I would) I get duplicate category links like this: Design Design Design Development + some empty lines How do I limit for eg, my Design category to just one? Or do I need to approach this differently? Thanks & regards Marty Link to comment Share on other sites More sharing options...
apeisa Posted October 4, 2012 Share Posted October 4, 2012 I don't understand how you page tree goes? What are posts and what are categories? Can you post a screenshot? It seems that you are looping through all the posts and getting categories that way -> you should loop just the categories. 1 Link to comment Share on other sites More sharing options...
Marty Walker Posted October 4, 2012 Author Share Posted October 4, 2012 Hi Antti, My page tree goes like this: -Notes -- post -- post -- post - Categories -- Notes --- Design --- Development --- etc But if I loop through the categories instead how do I list only the categories that are contained in posts? Regards Marty Link to comment Share on other sites More sharing options...
diogo Posted October 4, 2012 Share Posted October 4, 2012 This is a bit confusing because you have two pages called notes. Still, I will give it a try. Is this what you want? foreach ($pages->get("/category/")->children as $cat) { // find all pages with this category $withThisCat = $pages->get("/notes/")->find("note_category=$cat"); // if some pages have this category if($withThisCat->count() > 0){ // print the category link echo "<p><a href='{$cat->url}'>{$cat->title}</a></p>"; } } edit: added variables to make it more clear 1 Link to comment Share on other sites More sharing options...
Marty Walker Posted October 5, 2012 Author Share Posted October 5, 2012 Excellent. Thank you! I modified it a little and it works how I want it to. Noob question: One line I don't get is this one: <? if($withThisCat->count() > 0) ?> Why does it not echo a category multiple times? Regards Marty Link to comment Share on other sites More sharing options...
diogo Posted October 5, 2012 Share Posted October 5, 2012 if($withThisCat->count() > 0) checks if the array of posts where the category page is present is not empty this is what happens: Find all categories and loop through them | | Design <-- are there more than 0 posts with this category? yes! -> print it | Development <-- are there more than 0 posts with this category? yes! -> print it | One more <-- are there more than 0 posts with this category? no! -> don't print it | Last one <-- are there more than 0 posts with this category? yes! -> print it | | Leave the loop. Link to comment Share on other sites More sharing options...
Marty Walker Posted October 5, 2012 Author Share Posted October 5, 2012 Ahhhh! I've got it now. Thanks for the explanation. Regards Marty Link to comment Share on other sites More sharing options...
Marty Walker Posted October 5, 2012 Author Share Posted October 5, 2012 One more thing to finish this off would be to ask how I would go about highlighting the current category. Regards Marty Link to comment Share on other sites More sharing options...
apeisa Posted October 5, 2012 Share Posted October 5, 2012 In Diogo's example: <?php if ($cat->id == $page->id) echo "We are here"; Link to comment Share on other sites More sharing options...
Marty Walker Posted October 5, 2012 Author Share Posted October 5, 2012 Thanks again Antti. That pointed me in the right direction. I ended up with: <?php if($cat->name == $input->urlSegment2) echo " class='current'"; Regards Marty Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now