Call Intro or Full Article Image

General questions relating to Joomla! 5.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
Post Reply
joomlerGR
Joomla! Intern
Joomla! Intern
Posts: 53
Joined: Fri Apr 05, 2024 1:13 am

Call Intro or Full Article Image

Post by joomlerGR » Fri May 10, 2024 12:20 am

how to call Intro or Full Article Image inside Index.php into the content="..."

Code: Select all

<meta property="og:image" content="">

User avatar
carlitorweb
Joomla! Intern
Joomla! Intern
Posts: 68
Joined: Tue May 08, 2018 9:03 pm

Re: Call Intro or Full Article Image

Post by carlitorweb » Fri May 10, 2024 12:59 am

Not sure where is the "index.php", but normally this you can do it inside the article view. So, first, make a override to com_content/article.

Then you need the follow code:

Code: Select all

use Joomla\CMS\Factory; // this one can be already in your override
use Joomla\CMS\Uri\Uri;

$document = Factory::getApplication()->getDocument();
$images = json_decode($this->item->images);

$ogImg = !empty($images->image_fulltext) ? $images->image_fulltext : $images->image_intro;

if ( !empty($ogImg) ) {
    $document->addCustomTag('<meta property="og:image" content="' . Uri::base() . $ogImg . '" />');
}
The future will be automated, decentralized, and digital.

joomlerGR
Joomla! Intern
Joomla! Intern
Posts: 53
Joined: Fri Apr 05, 2024 1:13 am

Re: Call Intro or Full Article Image

Post by joomlerGR » Fri May 10, 2024 1:10 am

i implement it in default.php but its not working

any other idea?

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17510
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Call Intro or Full Article Image

Post by toivo » Fri May 10, 2024 1:32 am

Did you follow this tutorial to create the override: J4.x: Template Overrides?

In case you happened to modify the original file, components/com_content/tmpl/article/default.php and the template already had an override file, namely templates/your-template/html/com_content/article/default.php, the override file has precedence and the modified file default.php is never executed.
Toivo Talikka, Global Moderator

User avatar
carlitorweb
Joomla! Intern
Joomla! Intern
Posts: 68
Joined: Tue May 08, 2018 9:03 pm

Re: Call Intro or Full Article Image

Post by carlitorweb » Fri May 10, 2024 1:36 am

can you show the content of your default.php file? With the code I shared
The future will be automated, decentralized, and digital.

joomlerGR
Joomla! Intern
Joomla! Intern
Posts: 53
Joined: Fri Apr 05, 2024 1:13 am

Re: Call Intro or Full Article Image

Post by joomlerGR » Fri May 10, 2024 5:01 pm

screenshot attached below:
carlitorweb wrote:
Fri May 10, 2024 12:59 am
Not sure where is the "index.php", but normally this you can do it inside the article view. So, first, make a override to com_content/article.

Then you need the follow code:

Code: Select all

use Joomla\CMS\Factory; // this one can be already in your override
use Joomla\CMS\Uri\Uri;

$document = Factory::getApplication()->getDocument();
$images = json_decode($this->item->images);

$ogImg = !empty($images->image_fulltext) ? $images->image_fulltext : $images->image_intro;

if ( !empty($ogImg) ) {
    $document->addCustomTag('<meta property="og:image" content="' . Uri::base() . $ogImg . '" />');
}
You do not have the required permissions to view the files attached to this post.

User avatar
carlitorweb
Joomla! Intern
Joomla! Intern
Posts: 68
Joined: Tue May 08, 2018 9:03 pm

Re: Call Intro or Full Article Image

Post by carlitorweb » Fri May 10, 2024 5:17 pm

Did you check after in the frontend, inside the <head> section if the tag show there? Need be inside a article view, since was the file you override
The future will be automated, decentralized, and digital.

joomlerGR
Joomla! Intern
Joomla! Intern
Posts: 53
Joined: Fri Apr 05, 2024 1:13 am

Re: Call Intro or Full Article Image

Post by joomlerGR » Fri May 10, 2024 5:28 pm

yes i have checked and it wasnt there! did you tried it in your end to see if it works?

User avatar
carlitorweb
Joomla! Intern
Joomla! Intern
Posts: 68
Joined: Tue May 08, 2018 9:03 pm

Re: Call Intro or Full Article Image

Post by carlitorweb » Fri May 10, 2024 6:26 pm

I just checked, was missed the part of clean the image URL but even without it the tag show up well.

Correction:

Code: Select all

use Joomla\CMS\Factory; // this one can be already in your override
use Joomla\CMS\Uri\Uri;  // this one can be already in your override
use Joomla\CMS\HTML\HTMLHelper; // this one can be already in your override

$document = Factory::getApplication()->getDocument();
$images   = json_decode($this->item->images);

$ogImg = !empty($images->image_fulltext) ? $images->image_fulltext : $images->image_intro;

if ( !empty($ogImg) ) {
    $ogImg = HTMLHelper::_('cleanImageURL', $ogImg);
    $document->addCustomTag('<meta property="og:image" content="' . Uri::base() . $this->escape($ogImg->url) . '" />');
}
You do not have the required permissions to view the files attached to this post.
The future will be automated, decentralized, and digital.

joomlerGR
Joomla! Intern
Joomla! Intern
Posts: 53
Joined: Fri Apr 05, 2024 1:13 am

Re: Call Intro or Full Article Image

Post by joomlerGR » Fri May 10, 2024 8:03 pm

that code is working just fine!

so with that code we are showing the og:image meta tag at the article pages from intro image


so to evolve the code HOW WE CAN SET for all the other pages (main page, category and tag pages) to show the following og:image meta tag?

Code: Select all

<meta property="og:image" content="/MAIN-IMAGE.png"/>

User avatar
carlitorweb
Joomla! Intern
Joomla! Intern
Posts: 68
Joined: Tue May 08, 2018 9:03 pm

Re: Call Intro or Full Article Image

Post by carlitorweb » Fri May 10, 2024 8:21 pm

You can always set a $document->addCustomTag(), the thing is the image you will use.

Featured view you will need use a static image, since there you have a list of articles. Otherwise you will need get one of the article image from that list and use it in your og tag.

For category view is similar, a list of articles, but you have a param inside your category options for set a image for that category wich you can use for your og tag.

For example if you go to category/blog override you can use the follow to get the image

Code: Select all

$this->category->getParams()->get('image')
Then same as before, check if not empty, clean it and use it
The future will be automated, decentralized, and digital.

joomlerGR
Joomla! Intern
Joomla! Intern
Posts: 53
Joined: Fri Apr 05, 2024 1:13 am

Re: Call Intro or Full Article Image

Post by joomlerGR » Fri May 10, 2024 8:31 pm

at Articles: Edit Category if i set an image there: how that image will be shown in the

<meta property="og:image" content=..

?
You do not have the required permissions to view the files attached to this post.


Post Reply

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