Calling Intro or Full Article Image Inside Index.php

Everything to do with Joomla! 3.x templates and templating.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
supfort
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Oct 05, 2022 10:46 am

Calling Intro or Full Article Image Inside Index.php

Post by supfort » Wed Oct 05, 2022 10:57 am

I am using Joomla 3.10.11

###################
QUESTION

Does anyone knows how to call either "Intro Image" or "Full Article Image" inside the index.php using PHP code?

I know this can be done inside the articles default.php files or similar.

But I want to know if this can be accomplished inside the index.php

##################
EXAMPLE

For instance, I have the following HTML markup inside the index.php on

Code: Select all

<meta property="og:image" content="">
And I want to do something like below.

Code: Select all

<meta property="og:image" content="<?php echo $IntroImage = $doc->getIntroImage(); ?>">
##################
CALLING TITLES AND DESCRIPTION IS POSSIBLE

I know I can call titles and descriptions with the PHP codes below.

But I have not been able to find how this can be done with the images.

Code: Select all

<meta property="og:title" content="<?php echo $Title = $doc->getTitle(); ?>">

<meta property="og:description" content="<?php echo $Description = $doc->getDescription(); ?>">
I have attached my index.php if needed but below is the code as well.

Code: Select all

<?php

defined( '_JEXEC' ) or die( 'Restricted access' ); //Basic protection for PHP Joomla files
$doc = JFactory::getDocument();
$doc->setHtml5(true); //Change document to HTML5

/* THIS GET ALIAS INSIDE BODY CLASS FOR CUSTOM CSS */
$app = JFactory::getApplication();
$menu = $app->getMenu()->getActive();
$pageclass = ''; // Notice how the variable is empty first. Then place the code below.

if (is_object($menu))
$pageclass = $menu->params->get('pageclass_sfx');

?>
<!doctype html>
<html lang="<?php echo $this->language; ?>">
<head>

   <!-- HEAD GENERATED BEGINS -->
   <jdoc:include type="head" />
   <!-- HEAD GENERATED ENDS -->

   <!-- SOCIAL MEDIA TEST BEGINS -->
   <meta property="og:image" content="">
   <!-- SOCIAL MEDIA ENDS -->

   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta name="format-detection" content="telephone=no">
   <?php $this->setGenerator('Website powered by Engines'); ?>
   <link href="<?php echo JUri::current(); ?>" rel="canonical">
   <link rel="alternate" hreflang="<?php echo $this->language; ?>" href="<?php echo JUri::current(); ?>">
 
</head>
You do not have the required permissions to view the files attached to this post.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2441
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Calling Intro or Full Article Image Inside Index.php

Post by SharkyKZ » Wed Oct 05, 2022 11:34 am

Between the view layout override and the document/template, the layout override is the better place to do this. Template is not aware of the view data, including the intro image. You can fetch the data needed in the template but that's not really the right place to do it anyways, plus that will add additional database queries. You also shouldn't hardcode meta tags, use Document API instead. Best way is to use a content plugin. There are some plugins on JED for adding OG https://extensions.joomla.org/tags/open-graph/.

supfort
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Oct 05, 2022 10:46 am

Re: Calling Intro or Full Article Image Inside Index.php

Post by supfort » Wed Oct 05, 2022 11:53 am

I already find the solution for this.

################################
################################

I know people will say this is not the correct way.

But for my situation it is what I was looking for and needed.

So no further comments regarding the righteousness of what I am doing.

UNLESS what I am doing creates a security problem which in that case I will be more than welcome to hear other solutions.

################################
################################

Anyways, here is what I did.

I added the following code inside index.php

Code: Select all

$article = JTable::getInstance("content"); 
$article->load(JRequest::getInt("id"));
$article_images = $article->get("images");
$pictures = json_decode($article_images);
$images = json_decode($this->item->images);
$img=JURI::base() . $pictures->image_intro;
And then I added the code below inside the HTML markup

Code: Select all

<meta property="og:image" content="<?php echo $img; ?>">


Post Reply

Return to “Templates for Joomla! 3.x”