Jump to content

Recommended Posts

Posted

Hey all,

Is it possible to show the 'Author' of who created a particular page? For example, when creating a Blog article it would show the user who created this page (or most recently edited it) on the frontend. I've assumed it could work like this, but obviously the below code only shows the user who is currently logged in:

foreach($children as $child) {
echo "<h2><a href='{$child->url}'>{$child->title}</a></h2>";
echo "<p>{$child->summary}</p>";
echo "<p>Author {$user->name}</p>";
}

Cheers,

Sev

Posted

You can get user created and modified for a page with

<?php
$page->createdUser->name;
$page->modifiedUser->name;

$page would be $child in your code

  • Like 4
  • 4 years later...
Posted

I have tried it with PW 3.21 and it seems not to work any longer. I always get Error 500. :(

It works okay for me with PW 3.0.21. Not sure why it's not working for you but as an alternative you could use:

$users->get($page->settings->created_users_id)->name
$users->get($page->settings->modified_users_id)->name
Posted

Doesnt work either for me. I have tested it with Tracy debugger tool and I always get an error message with pageimage after adding the line of code to the template????

Exception: Method Pageimage::last does not exist or is not callable in this context (in ........web/wire/core/Wire.php line 409)

I dont know why because this has nothing to do with the user name. Maybe I have to dive a little bit deeper into my template code.

Posted

Do you do anything in hooks related to images (maybe on the user template)? Also Tracy will probably give you a stack trace, which might be helpful to debug this further.

Posted

No I dont hook into image fields. I only have a textformatter running (without a hook) which manipulates the image class of images added with the CKEditor.

The callstack shows me always an error on the index.php.

post-2257-0-26029600-1465754015_thumb.jp

Posted

Ok I have traced the problem down to my userimage. There must be the problem. The API of the userimage seems to be wrong. If I comment out the userimage, the name of the creator will be displayed properly. :)

So the API call as described above works also in 3.0.21.

Posted

That's expected to error there. The interesting part is where the exception did come from earlier in the call stack. And maybe look at that textformatter. There's some code calling last() on an image, while the object is probably supposed to be a list of images.

Posted

Just use $user->getUnformatted('userimage') and you'll get a Pageimages object no matter what. It's less error prone especially as the output-formatting on user objects is sometimes a bit iffy about when it's on or not.

Posted

Oh it seems that the size attribute in the api call is responsible for the error.

This works:

$user->userimage

but this works not:

$user->userimage->size(75, 75, array('cropping' => 'outside'))->url

Settings of my userimage field:

post-2257-0-01423200-1465794680_thumb.jp

Error message with Tracy:

post-2257-0-73841100-1465794738_thumb.jp

Posted

$user objects are not always (never??) in output formatting ON mode like all other pages are – even on the frontend. And as long as output formatting is off these field settings are ignored and you'll always get a list of images. So in the case of working with the user object it's wise to force a specific formatting:

$user->getFormatted('image_field'); // Returns regarding to the fields setting
$user->getUnformatted('image_field'); // Returns always a list (does not break when field settings would change)
  • Like 2
Posted

You are absolutely right!! This code works:

$user->getFormatted('userimage')->size(75, 75, array('cropping' => 'outside'))->url;

I didnt know that user fields are specific on frontend, because I never had troubles before.

Thank you so much! Now everything works as expected.

  • 5 years later...
Posted
On 11/3/2011 at 5:04 PM, Soma said:

You can get user created and modified for a page with

 

<?php
$page->createdUser->name;
$page->modifiedUser->name;
 

$page would be $child in your code

Is there a documentations for all these things? 

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