fetch user custom fields in article

by niaziblog   Last Updated May 14, 2019 16:10 PM

I have created some custom fields in user profile from backend

templates/name-template/html/com_content/article/default.php

and use this code for load custom fields at joomla article

<?php 
$context = 'com_content.article';
$article = $this->item;

//use the field helper class to get the fields
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$fields = FieldsHelper::getFields($context, $article, true);
// Added so as to access fields by their name

// GET CUSTOM FIELDS
foreach($fields as $field) {
    $fields[$field->name] = $field;
}
?>

and use this code for fetch

<?php echo ($fields['name-field']->value); ?>

i creat textarea fileds at user profile for write about me (author) enter image description here now I want to fetch users custom fields (author) in joomla article How can i do it ؟



Answers 1


Change context to com_users.user and the item to article author's user object. Since some articles may not have an author, this code should be wrapped in author check:

// Only run if article has an author.
if ($this->item->created_by)
{
    // Get author's custom fields.
    $fields = FieldsHelper::getFields('com_users.user', JFactory::getUser($this->item->created_by), true);

    // Display fields here.
}
Sharky
Sharky
May 14, 2019 16:32 PM

Related Questions




Custom form field not working on server

Updated April 26, 2017 10:10 AM