Jump to content

Recommended Posts

Posted

Hello! I have a number of blogs, and blog post uses a page input field to select an author.

For each blog, I want to create a list of authors who have contributed to that blog. 

Here's the structure:

Authors

- Don

- Peggy

- Pete

Blogs

- Blog 1

- Blog 2

I can do this on the blog template:

foreach ($page->children->find('author!=""') as $child) {
echo $child->author->title;				
}

...but that lists duplicates (i.e. Don Don Don Peggy). I'd like to echo each author only once. 

Thanks!

Posted

I don't think I completely follow - if you are on a page with a single blog post, then $page should refer to that one post. I am not sure why you are selecting the children of the page. 

However, if you are on a parent blog page showing more than one post, then you'd need to limit your selector to each specific post.

If neither of those make sense for what you're trying to do, you could get really ugly and populate an array inside your foreach and only echo the author if it is not already in the array:

$authors = array();
foreach ($page->children->find('author!=""') as $child) {
    if(!in_array($child->author->title, $authors)){
        echo $child->author->title;
    }
    $authors[] = $child->author->title;
}

But you really shouldn't have to do that - I think you just need to get your selector specific to your post.

  • Like 1
Posted

Thanks for the quick reply! Yes, the ugly solution works.

This is on the blog template, so yep, the children are the blog 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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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