Your Ad Here

Saturday, December 3, 2011

Joomla 1.5 - Add a new module position inside an article

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
Add a new position, something like
adsense_in_content
.
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.

No comments:

Post a Comment