Jump to content

Listing authors by blog


caribou
 Share

Recommended Posts

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!

Link to comment
Share on other sites

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
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...