Jump to content
Marty Walker

Another 'categories' question

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

In Diogo's example:

<?php
if ($cat->id == $page->id) echo "We are here";

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...