Rendering custom fields in module latest news

General questions relating to Joomla! 3.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Locked
danismar2018
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sat Apr 14, 2018 12:06 am

Rendering custom fields in module latest news

Post by danismar2018 » Thu Apr 19, 2018 2:07 am

Dear,

I've created some custom fields to integrate into a certain category of articles. These articles are displayed in a customized module of latest news. I would like to display these custom fields in the module, but I can not. Could someone help me in this matter ???

Thank you!

User avatar
effrit
Joomla! Guru
Joomla! Guru
Posts: 846
Joined: Sun Nov 12, 2017 2:21 pm
Location: middle of Russia
Contact:

Re: Rendering custom fields in module latest news

Post by effrit » Thu Apr 19, 2018 4:45 am

hi
you must make database request.
you can do it by php or using Joomla classes but it's still DB request...

for single article with id = $id

Code: Select all

$db = JFactory::getDbo();
$query = 'select * from #__fields_values where item_id = "'.$id.'"';
$db->setQuery($query);
$fields = $db->loadObjectList();

by framework

Code: Select all

        $item->jcfields = FieldsHelper::getFields('com_content.article', $item, true);
        $fields = [];
        foreach($item->jcfields as $jcfield)
        {
            $fields[$jcfield->name] = $jcfield;
        }

        var_dump ($fields);

sanji
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Tue Apr 10, 2018 3:09 am

Re: Rendering custom fields in module latest news

Post by sanji » Thu Apr 19, 2018 7:10 am

effrit wrote:hi
you must make database request.
you can do it by php or using Joomla classes but it's still DB request...

for single article with id = $id

Code: Select all

$db = JFactory::getDbo();
$query = 'select * from #__fields_values where item_id = "'.$id.'"';
$db->setQuery($query);
$fields = $db->loadObjectList();

by framework

Code: Select all

        $item->jcfields = FieldsHelper::getFields('com_content.article', $item, true);
        $fields = [];
        foreach($item->jcfields as $jcfield)
        {
            $fields[$jcfield->name] = $jcfield;
        }

        var_dump ($fields);
Ok, but how do we do it? Do we need to create a module?

User avatar
effrit
Joomla! Guru
Joomla! Guru
Posts: 846
Joined: Sun Nov 12, 2017 2:21 pm
Location: middle of Russia
Contact:

Re: Rendering custom fields in module latest news

Post by effrit » Thu Apr 19, 2018 7:28 am

the topic is about "Rendering custom fields in module latest news" so you must create new template for this module (or override in template for it)and paste first or second example code it it in loop there items are shown

danismar2018
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sat Apr 14, 2018 12:06 am

Re: Rendering custom fields in module latest news

Post by danismar2018 » Thu Apr 19, 2018 12:26 pm

effrit wrote:hi
you must make database request.
you can do it by php or using Joomla classes but it's still DB request...

for single article with id = $id

Code: Select all

$db = JFactory::getDbo();
$query = 'select * from #__fields_values where item_id = "'.$id.'"';
$db->setQuery($query);
$fields = $db->loadObjectList();

by framework

Code: Select all

        $item->jcfields = FieldsHelper::getFields('com_content.article', $item, true);
        $fields = [];
        foreach($item->jcfields as $jcfield)
        {
            $fields[$jcfield->name] = $jcfield;
        }

        var_dump ($fields);




OK! More then I have the following situation: Each article will have a different custom field value. How to redenricionar the respective field in the module?

Code: Select all

	<ul class="latestnews<?php echo $moduleclass_sfx; ?>">
<?php foreach ($list as $item) : ?>
	<li itemscope itemtype="https://schema.org/Article">
		<a href="<?php echo $item->link; ?>" itemprop="url">
			<span itemprop="name">
				<?php echo $item->title; ?>
			</span>
		</a>
	</li>
<?php endforeach; ?>

</ul>

User avatar
effrit
Joomla! Guru
Joomla! Guru
Posts: 846
Joined: Sun Nov 12, 2017 2:21 pm
Location: middle of Russia
Contact:

Re: Rendering custom fields in module latest news

Post by effrit » Thu Apr 19, 2018 12:56 pm

paste in any place inside foreach.
after <li itemscope itemtype="https://schema.org/Article">, for example.

Code: Select all

<?php
		$id=$item->id;
		$db = JFactory::getDbo();
		$query = 'select * from #__fields_values where item_id = "'.$id.'"';
		$db->setQuery($query);
		$fields = $db->loadObjectList();

		foreach ($fields as $field) {
			if ($field->field_id == 2) { // 2 - id of your custom field
			echo $field->value;
			}
		}
		?>

diinekis
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 170
Joined: Wed Dec 03, 2008 9:02 am

Re: Rendering custom fields in module latest news

Post by diinekis » Tue Mar 03, 2020 11:46 am

Hi,

Above code renders the CF value, how can I show also the custom field label?

Thank you.


Locked

Return to “General Questions/New to Joomla! 3.x”