6

I am trying to customize the the category list of my website. So I have a menu option that shows the list of all article is a certain category. In this list I would like to show the into image of the article too. But unfortunately I cannot find the path of this intro image of each article to put it in the '' tag.

This code takes the first image in the article. Which is not what I want.

<?php
$text = $article->introtext;
if(preg_match('/<\s*img[^>]+>/Ui', $text, $matches)){

   $image=$matches[0];
   $text = str_replace($image,'',$text);
}
if ($matches) {
$image = str_replace('img src="','img width="150px" src="/demo/persianlondon/',$image);
$image = str_replace("caption","", $image);
echo '' . $image . '';
}
else {echo '<img width="150px" src="http://goo.gl/k47rNN" class="defaultArticleImage" />';}

?>

So I need to get the intro image url and set it for $image. Any idea to do it?

  • To give your question context, please provide some sample data and your expected result. What does $article->introtext contain? I am sure I can provide you with an optimized solution if you can clarify the posted question. – mickmackusa Nov 30 '18 at 1:10
12

That code is looking in the article text you need to is in the images call. It is json encoded so you have to decoded it first.

$images  = json_decode($this->item->images);

echo $images->image_intro;

Have a look at the com_content/article/default.php template to see some of the other parts of data you can use for images.

  • Thank you for you post. I just couldnt get it working. Can you provide the full code, or just tell me where should I add it to? – Danial Jul 31 '14 at 15:08
  • 1
    Try variations on the object as I am not sure the code in the template you are using: $article->images or $item->images or $article->params->get('images') it will be around there somewhere on the main object – tristanbailey Sep 13 '14 at 22:30
  • The above is what I used. Don't forget to add the php tags obviously. I did it this way. <?php $images = json_decode($this->item->images); $introImage = $images->image_intro; ?> ...src="<?php echo $introImage ; ?>".... – Eoin May 24 '16 at 16:38
1

I had the same problem on twitter. I was trying to add my link picture just as facebook does automatically but had to investigate the CARD Validator here https://cards-dev.twitter.com/validator ...Tthe way I have it working on Joomla 3.9.11 is adding this code in the head of my template default.php ... I was able to properly add the title, the description and the right picture, usually the first one on the article.

<head>

<?php 
//Added for TWITTER
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
    $ids = explode(':',JRequest::getString('id'));
    $article_id = $ids[0];
    $article =& JTable::getInstance("content");
    $article->load($article_id);
    $theArticle = $article->get("title");
    $theImages = $article->get("images");   
    $pictures = json_decode($theImages); // Split the parameters apart
    $timage= "http://yourdomain.com/".$pictures->{'image_intro'};
}
$doc =& JFactory::getDocument();
$doc->addCustomTag( '
<meta name="twitter:title" content="'.mb_strimwidth(strip_tags($theArticle),0,225, " ...").'"=""/>
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="yoursite">
<meta name="twitter:creator" content="yourcreator">
<meta name="twitter:url" content="'.JURI::current().'">
<meta name="twitter:description" content="'.mb_strimwidth(strip_tags($doc->getMetaData( 'description' )),0,225, " ...").'"=""/>
<meta name="twitter:image" content="'.$timage.'">
');
?>
    <jdoc:include type="head" />
    <?php $this->loadBlock('head') ?>

</head>
New contributor
Luis H Cabrejo is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Thank you for sharing Luis and welcome to JSE. Please take the tour when you can. I recommend that you either remove the if statement if it is unnecessary, or add the $doc->addCustomTag() block inside of it. If you are coding on a "good" IDE, it will be flagging your code to state that some variables are potentially undeclared. Also, if you know that there will be a : in the id string, $article_id = strstr(JRequest::getString('id'), ':', true); is a more direct technique than creating a temporary array and accessing the first element. Consider these notes: 3v4l.org/qvj0M – mickmackusa Oct 12 at 1:29
0

The above works for me in the category blog view, it might be different in category list view. Don't forget to add the php tags obviously. You could always use <?php // print_r($images); ?> to print all of the available options which might give you a clue. Or potentially this <?php // print_r($this->item); ?>

I did it this way.

<?php
$images         = json_decode($this->item->images); 
$introImage   = $images->image_intro;
?>

...src="<?php echo $introImage ; ?>"....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.