How to show first article image instead of intro image Topic is solved

General questions relating to Joomla! 4.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
User avatar
sanal
Joomla! Apprentice
Joomla! Apprentice
Posts: 28
Joined: Sat Dec 22, 2007 11:49 am

How to show first article image instead of intro image

Post by sanal » Sat Jun 03, 2023 11:53 am

My question is how show the first image present in the article content if no intro image has been added?
External images within the article should also be viewable.

Thanks
:pop
Last edited by toivo on Sat Jun 03, 2023 9:27 pm, edited 1 time in total.
Reason: mod note: moved, not related to 4.x Coding
http://www.joomlacms.eu.org Joomla Özel Destek Sitesi

User avatar
Pavel-ww
Joomla! Ace
Joomla! Ace
Posts: 1346
Joined: Tue Jun 30, 2020 12:17 pm

Re: How to show first article image instead of intro image

Post by Pavel-ww » Sun Jun 04, 2023 8:00 am

Hi. Add "read more" after image

User avatar
sanal
Joomla! Apprentice
Joomla! Apprentice
Posts: 28
Joined: Sat Dec 22, 2007 11:49 am

Re: How to show first article image instead of intro image

Post by sanal » Sun Jun 04, 2023 2:00 pm

Hello
Thank you for your answer.
I opened the topic in the coding section, but it has been moved here.
If there is no intro image, the first image of the article will be displayed.
Probably the intro_image.php file needs to be overridden.
But I don't know how to do it.

Thanks
http://www.joomlacms.eu.org Joomla Özel Destek Sitesi

User avatar
Pavel-ww
Joomla! Ace
Joomla! Ace
Posts: 1346
Joined: Tue Jun 30, 2020 12:17 pm

Re: How to show first article image instead of intro image

Post by Pavel-ww » Sun Jun 04, 2023 5:39 pm

Hi. "the first image of the article" implies the use of the image placed inside article (added into articlr editor). In this case you should use "read more".

If you mean the full article image (its location is our of article body) as an intro image , then this works just like that out of the box. If the intro image is not set, then the full article image is shown in article list (blog layout).

Here is default intro_image.php:
1.jpg
--

Therefore, I do not understand what you want.
You do not have the required permissions to view the files attached to this post.

ProWebKong
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Mon Jun 05, 2023 3:40 pm

Re: How to show first article image instead of intro image

Post by ProWebKong » Mon Jun 05, 2023 3:44 pm

In Joomla, if no intro image has been added to an article, you can display the first image present in the article content by modifying the template or using an extension. Here's how you can achieve this:

Modify the template:
a. Locate the template file responsible for rendering the article view. It is typically located in templates/your_template/html/com_content/article/default.php.
b. Within this file, find the section that handles the display of the intro image. It may look something like this: <div class="intro-image">...</div>.
c. Below or within this section, you can add code to retrieve the first image from the article content.

User avatar
sanal
Joomla! Apprentice
Joomla! Apprentice
Posts: 28
Joined: Sat Dec 22, 2007 11:49 am

Re: How to show first article image instead of intro image

Post by sanal » Mon Jun 05, 2023 5:34 pm

Thank you for your interest.
There is a code that displays the first image in the article.
I tried to use the code in blog_item.php.

Code: Select all

<?php $show_images = LayoutHelper::render('joomla.content.intro_image', $this->item); ?>
<?php if(!isset($images->image_intro) || empty($images->image_intro)) { ?>
<div class="no-image">
<?php } ?>
<?php $texthtml=$this->item->text; 
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $texthtml, $image);
 $imgfirst=$image['src'];
 echo '<img src="' . $imgfirst. ' ">'
?>
<?php if(!isset($images->image_intro) || empty($images->image_intro)) { ?>
</div>
<?php } ?>
Such an image emerged.
If there is an intro image in the article, two images were displayed at once.

Image

If there is no intro image in the article, I want the first image in the article to be displayed, if there is, it should not be displayed.
I think this condition is in intro_image.php file.

Maybe the condition is this code.

Code: Select all

if (empty($images->image_intro)) {
    return;
}
Thanks
http://www.joomlacms.eu.org Joomla Özel Destek Sitesi


User avatar
Pavel-ww
Joomla! Ace
Joomla! Ace
Posts: 1346
Joined: Tue Jun 30, 2020 12:17 pm

Re: How to show first article image instead of intro image

Post by Pavel-ww » Tue Jun 06, 2023 9:33 am

sanal wrote:
Mon Jun 05, 2023 5:34 pm
Maybe the condition is this code.

Code: Select all

if (empty($images->image_intro)) {
    return;
}
Hi. You're right. You need to write a condition in which the first image will be shown. But before writing the code, you must solve this logical problem in the mind and draw a block scheme.

Translation from PHP to human :)
If (intro image is not set or empty)> do something...

You need to find a logical answer and then write it in the code.

User avatar
sanal
Joomla! Apprentice
Joomla! Apprentice
Posts: 28
Joined: Sat Dec 22, 2007 11:49 am

Re: How to show first article image instead of intro image

Post by sanal » Tue Jun 06, 2023 10:07 am

Hello
Thank you for your answer.
I don't know how to write php code, if I knew, I would have a chance to try the options.
I don't even know English, I use google translate.
I can only express the condition in words, I don't know if it has an equivalent as php code.
The condition is simply;
1. If there is no article intro image,
2. Consider the first image in the article as the article introduction image.
So first image in article = article intro image

Thanks
http://www.joomlacms.eu.org Joomla Özel Destek Sitesi

User avatar
sanal
Joomla! Apprentice
Joomla! Apprentice
Posts: 28
Joined: Sat Dec 22, 2007 11:49 am

Re: How to show first article image instead of intro image

Post by sanal » Wed Jun 14, 2023 1:21 am

Hello
The issue has been resolved, thank you to everyone involved.
You do not have the required permissions to view the files attached to this post.
http://www.joomlacms.eu.org Joomla Özel Destek Sitesi

User avatar
Nidzo2203
Joomla! Explorer
Joomla! Explorer
Posts: 279
Joined: Sat Nov 21, 2009 4:52 pm
Contact:

Re: How to show first article image instead of intro image

Post by Nidzo2203 » Wed Jun 14, 2023 3:41 am

How?


Post Reply

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