Jump to content

Page class properties returned with different values


arizanov
 Share

Recommended Posts

Hi guys. I am new to PW so I need some help. I can't figure out something basic here I think. I have the following code in the template file:

namespace ProcessWire;

$content = $page->body;
$posts    = $page->children("limit=10");

if(!empty($posts)):
    foreach($posts as $post):
        $content .= renderBlogItem($post);
    endforeach;
else:
    $content .= "<h3>No articles found</h3>";
endif;

$pagination = $posts->renderPager();
$content .= $pagination;

And the following code in renderBlogItem function:

function renderBlogItem($post){
    return    '<div class="item">
                <h2> <a href="'.$post->url.'">'.$post->title.'</a></h2>
<div>'. substr(strip_tags($post->body), 0, 375) .'...</div>
<p><a href="'.$post->url.'">Read full story &gt;</a></p>
<span>'.$post->createdUser.'</span>
</div>
';
}

The issue is that fields are outputed differently from their actual values, for example for createdUser I have printed a number, for createdDate I have timestamp instead of the full date I have when I print the $post variable. What am I missing here ? 

Thanks.

Link to comment
Share on other sites

Thats the normal output of those variables.

You can try for user this. The way you are outputting that value it just outputs the ID of the user. Try this to get the username:

$post->createdUser->name

And if createdDate is a Datetime field, you should have output formatting configured in your Details tab of the field configuration so that it actually formats the value as a string otherwise print the unix timestamp.

Link to comment
Share on other sites

Hmm. In my case createdUser property is a string, not a object of a class. The whole page class object looks like this: 


object(ProcessWire\Page)[215]
  public 'instanceID'   
  public 'id'   
  public 'name'    (length=20)
  public 'path'    (length=27)
  public 'status'    (length=0)
  public 'template'    (length=10)
  public 'parent'    (length=6)
  public 'numChildren'   
  public 'sort'   
  public 'sortfield'    (length=4)
  public 'created'    (length=32)
  public 'modified'    (length=32)
  public 'published'    (length=32)
  public 'createdUser'    (length=14)
  public 'modifiedUser'    (length=14)
  public 'isLoaded'   
  public 'outputFormatting'   
  public 'hooks'  
    array (size=12)
      'before Page::render'    (length=51)
      'Page::render'    (length=45)
      'Page::editable'    (length=53)
      'Page::publishable'    (length=56)
      'Page::viewable'    (length=53)
      'Page::listable'    (length=53)
      'Page::deleteable'    (length=55)
      'Page::deletable'    (length=55)
      'Page::trashable'    (length=54)
      'Page::addable'    (length=52)
      'Page::moveable'    (length=53)
      'Page::sortable'    (length=53)
  public 'data'  
    array (size=2)
      'title'    (length=20)
      'summary'    (length=0)
Link to comment
Share on other sites

Did you tried to debug the property directly? I am getting a User object just fine. I know it shows as a string in the Page object, but if you debug that property it returns a user object (at least from what I can tell in my installations)

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