Jump to content

Link to author's page? (profile pages)


Patrik
 Share

Recommended Posts

Hi,

In short, what I'm looking for is profile pages for all registered users, that are visible to anyone visiting the site.

Long version:

I would like to link to the author's (user) page from a blog entry. I'm using the createdUser object to retrieve the user name, and then construct a link that looks something like this: /author/{$page->createdUser->name}

Creating the link is obviously no problem, but how do I make that actual page exist without creating pages manually for all the users?

Apologies if this topic has been up before. I have searched the forums, bit couldn't find an anwser.

Regards,

Patrik

Link to comment
Share on other sites

Hi patrik,

You can use urlSegments for this case.

In your template used by the /author/ page, enable urlSegments in the Template config.

Then in the template file (example code):

if ($input->urlSegment1) {
  $username = $sanitizer->selectorValue($input->urlSegment1);
  // Maybe you need to add 'check_access=0' to your selector if also guest users can see this page...
  $u = $users->get($username);
  if ($u->id) {
    // Display the information about user
  } else {
    throw new Wire404Exception();
  }
}

// No urlSegment passed - throw a 404 or output some message
  • Like 3
Link to comment
Share on other sites

With url segments (http://processwire.com/api/variables/input/ scroll to the middle of the page). Allow them on the authors page template, and on this template's file put something like this:

if ($input->urlSegment1) {

    ​$username = sanitizer->name($input->urlSegment1);
    $userpage = $pages->get(name=​$username, template=user, include=all);

    if ($userpage->id) {

        // echo the user info. these would be custom fields added to the "user" template
        echo "<h2>{$userpage->first_name} {$userpage->last_name}</h2>";
        echo "<p>{$userpage->bio}</p>";
    }
}

You can add any info you want to the users pages by adding fields to the "user" template (on the templates page, under "filters", enable "show system templates?") 

Edit: Wanze beat me again. He beats everyone lately ;)

Edit2: corrected my code to make the check for guest visitors that Wanze refered, except that I'm using $pages instead of $users to get the user page

  • Like 3
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...