I have a Joomla! website and I need to make the article default.php file to automatically add some custom code (Google Adsense or any other custom module generated content) after a specific tag (Eg.: second
or third ) of the article html code.
To be short, I have the standard php call for article text:
<div itemprop="articleBody">
<?php echo $this->item->text; ?>
</div>
I want to use make an variable, let's call it $basic_article
, which will be populated with $this->item->text;
I want to create a function that will count the number of paragraphs from $basic_article
and insert a rendered Joomla! module position or even custom html code before or after the second, third or x paragraph of the string, then rewrap the string and print it or echo it.
Please, help me! Any advice is wellcome.
I have done this with advertising blocks by inputting a module position between set paragraphs. Then you can assign which pages have that custom code (ie in module) on it or not. You can take this and customize to your needs.
Here is an example to put module between 1st and 2nd paragraphs. You would replace your code with this
<div itemprop="articleBody">
<?php
$needle = "</p>";
$length = strlen($needle);
$this_article = $this->item->text;
$pos1 = strpos($this_article, $needle);
$pos2 = ($pos1!== 0 && strpos($this_article, $needle, $pos1 + $length)) ? strpos($this_article, $needle, $pos1 + $length): 0;
$first_para = substr($this_article,0,$pos2);
$rest_para = substr($this_article,$pos2 + $length);
echo $first_para;
echo JHtml::_('content.prepare', '{loadposition inline-zone}');
echo $rest_para;
?>
</div>
You want to either do this in a template override in /templates/MY_TEMPLATE_NAME/html/com_content/article/default.php
Or make a custom article layout and put it in /components/com_content/views/article/tmpl/ . For example, I made copies of default.php and default.xml, renamed them inline.php and inline.xml and uploaded after customizing.
Make sure you assign the module you want interjected into that position name (inline-zone in example) and assign to the correct page(s).