Joomla 1.5 - add a new module position inside an article
Today i received a request to insert a new module position inside the article template of a joomla website. The client wanted to print an adsense module in that position. You can see the result in the image below.
To do this, you need to define a new position first, so go to /templates/your_template. Open the templateDetails.xml file and search for <positions> Add a new position, something like <position>adsense_in_content</position>.
Save the file, and upload it back to it's place.
Ok, now go to /components/com_content/views/article/tmpl and open the default.php file.
Somewhere near line 120 you will see this code :
<?php if (isset ($this->article->toc)) : ?> <?php echo $this->article->toc; ?> <?php endif; ?>
After this code, do something like this:
<?php $article = $this->article->text; $countchars = strlen($article); $divide = $countchars / 2; $firstpart = substr($article, 0, $divide); $secondpart = substr($article, $divide); ?>
So, we took the article and count the characters inside it, divide that amount to 2 (We need 2 parts. Between them we will add the new position code.) and then add the first part in the variable named $firstpart and the second part in a variable $secondpart.
Now, echo the first part of the article:
<?php echo $firstpart; ?>
Now, we will insert the new module position:
<div style="float:left; padding-top: 5px; padding-right: 5px; padding-bottom: 5px;">
<?php
$myblurb_modules = &JModuleHelper::getModules( 'your_module_position' );
foreach ($myblurb_modules as $myblurb) {
$_options = array( 'style' => 'xhtml' );
echo JModuleHelper::renderModule( $myblurb, $_options );
}
?>
</div>The module will float to the left part of the page, as shown in the picture. Float it to right if you want, or change it as you wish. Don't forgot to change the 'your_module_position' with your new module position name.
Now, we only have to echo the second part of the article:
<?php echo $secondpart; ?>
That's it. Now go to your "module manager" in joomla admin and add what module you want to this new position.
< ? php echo $this->article->text; ? >, the article displays fine. Have tried different modules in this position as well to no avail - any ideas??Post new comment