Jump to content

Another 'categories' question


Marty Walker
 Share

Recommended Posts

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

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.

  • Like 1
Link to comment
Share on other sites

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

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

  • Like 1
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...