Changing "Posted by" in Standard Blog.

Permalink 1 user found helpful
Hi Guys & Gals,

I need some help. I am importing over 90 articles into a new C5. I'm currently using the standard blog. I have assigned the authors of each article as the page owners but since I added all of the posts it shows my name or the editors name.

Is there away to change is so that it only shows the page owners name as the "Posted by"?

Thanks in advance

uclageek
 
mesuva replied on at Permalink Reply
mesuva
Try this code instead of the line that has getVersionAuthorUserName:

$page = Page::getCurrentPage();
$ui = UserInfo::getByID($page->getCollectionUserID());
echo $ui->getUserName();


According to
http://www.concrete5.org/documentation/developers/pages/overview...
getCollectionUserID should return the ID of the page owner OR the page owner if it has been changed. A quick test worked for me, it showed the owner of the page I selected in the page properties.

Refer to:
http://www.concrete5.org/documentation/developers/permissions/users...
If you want to perhaps grab a user attribute (like a full name) instead of the username.
uclageek replied on at Permalink Reply
uclageek
Hi,

Thanks a lot that work for the individual pages but when I tried it for the
page lists it errored out.

Mike
jordanlev replied on at Permalink Reply
jordanlev
Can you post the code you tried for the page list?
uclageek replied on at Permalink Reply
uclageek
I tried 2 different ways.

foreach ($cArray as $cobj):
                $page = Page::getCurrentPage();
                $ui = UserInfo::getByID($page->getCollectionUserID());
                $title = $cobj->getCollectionName();
                $date = $cobj->getCollectionDatePublic('F j, Y');
                $author = $cobj->echo $ui->getUserName();
                $link = $nh->getLinkToCollection($cobj);
                $firstClass = $isFirst ? 'first-entry' : '';
                $entryController = Loader::controller($cobj);
                $comments = $entryController->getCommentCountString('%s
'.t('Comment'), '%s '.t('Comments'));
                $isFirst = false;
        ?>


and

foreach ($cArray as $cobj):
                $title = $cobj->getCollectionName();
                $date = $cobj->getCollectionDatePublic('F j, Y');
                $author = $cobj->$page = Page::getCurrentPage(); $ui UserInfo::getByID($page->getCollectionUserID()); echo $ui->getUserName();
                $link = $nh->getLinkToCollection($cobj);
                $firstClass = $isFirst ? 'first-entry' : '';
                $entryController = Loader::controller($cobj);
                $comments = $entryController->getCommentCountString('%s
'.t('Comment'), '%s '.t('Comments'));
                $isFirst = false;
        ?>


Thanks for all your help
jordanlev replied on at Permalink Reply
jordanlev
Both of your code samples have errors in them.

In your first example, you're assigning the currently-viewed page to the $page variable (that is, the page that the viewer is currently looking at, *not* the page that you're listing in the page list loop). So you need to change this:
$page = Page::getCurrentPage();
$ui = UserInfo::getByID($page->getCollectionUserID());

...to this:
$ui = UserInfo::getByID($cobj->getCollectionUserID());


And then further down you have another error, so you need to change this:
$author = $cobj->echo $ui->getUserName();

...to this:
$author = $ui->getUserName();


(Note that when you're inside that loop, the $cobj variable is the "page" that is being listed -- I wish it were just called "$page" as that would make more sense, but $cobj is short for "Collection Object", because in C5 pages are also called Collections inside the codebase)


For your second example, you would need to change this:
$author = $cobj->$page = Page::getCurrentPage(); $ui UserInfo::getByID($page->getCollectionUserID()); echo $ui->getUserName();

...to this:
$author = UserInfo::getByID($cobj->getCollectionUserID())->getUserName();
uclageek replied on at Permalink Reply
uclageek
Hi Thanks for all your help.

Everything work except now when I try to edit the defaults I get this
error.

*Fatal error*: Call to a member function getUserName() on a non-object in
...*/blog_entry.php* on line *25*
*
*
*Line 25 looks like this
*

<p class="meta"><?php echo t('Written by')?> <?php $page Page::getCurrentPage(); $ui UserInfo::getByID($page->getCollectionUserID()); echo $ui->getUserName(); ?>
on <?php echo
$c->getCollectionDatePublic('F j, Y'); ?></p>

--Mike
AngusHume replied on at Permalink Reply
AngusHume
Me too. Works ok on the live pages, but on my blog default page the same fatal error.

Posted by 
Fatal error: Call to a member function getUserName() on a non-object in /Applications/MAMP/htdocs/concrete5/themes/w4e001/blog_entry.php on line 19

Line 19 is
<p class="meta"><?php   echo t('Posted by')?> <?php  $page = Page::getCurrentPage(); $ui = UserInfo::getByID($page->getCollectionUserID()); echo $ui->getUserName(); ?> on <?php   echo $c->getCollectionDatePublic('F j, Y'); ?>  |  <span class="fblike"><fb:like send="false" layout="button_count" width="180" show_faces="false" style="vertical-align:top;zoom:1;*display:inline"></fb:like></span></p>