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.
Comments
flash in one article
Thank you very much for the nice tutorial. It worked perfectly. Is it possible to insert the flash module only in selected articles? If yes, could you please tell me how.
articletitle
Thanks, i am glad it helped.
For your question. You can use the variable.
Something like this:
if ($articletitle == 'The name of the article where you want to print the flash module') {
php stuff for calling the flash module;
}
else {
print the module that you want to replace the flash module when not printed.. or don't print nothing;
}
?>
Hope that helps.
flash
Fortunately there was no need to play around with the code. I was ignorant about the ability of joomla to embed media in articles. It was possible to do it just by clicking the 'embed media' button in the article editor. But thanks anyway.
Adding a new position
This is a great article. Just a tip - in 1.5, you can add new positions by simply typing them into the "Position Name' dropdown, when editing whatever module will go in there. Once you hit save, the postion is created. No need to go the template xml.
Thanks
Thanks for the info Rob, i didn't know that. It seems to be the fastest way to create a new position.
Again, thanks.
Can't get module to load --
Have tried different names in the XML and the default.php - neither have worked. The division works fine, and once I removed
< ? php echo $this->article->text; ? >, the article displays fine. Have tried different modules in this position as well to no avail - any ideas??Another way to include module inside content (Version 1.5)
There is another way to load a module inside an article.
Define a new position for the module. Select the module in the module list and type into module position drop-down. E.g. "user01" is the new position.
In the article, include the tag {loadposition user01}. Enclose the tag inside a div (in HTML view of the editor) with style set to float right OR left as necessary.
The module will load inside the article.
This works on Version 1.5. Not sure if it does on 1.0.
Great article.. BTW, what font are you using on this site?
Great post! Steve has a good suggestion for those positions that are needed for just one instance, or two.. but when you want to add positions to the template itself, your article is great.
BTW, what font are you using for this site, it looks killer!?? You've got my email,.. drop me a line,.. after all,.. I replied to you ah! :D
Steve is right, it's a
Steve is right, it's a simpler way of doing it. The difference is that i wanted the position to be exactly at the middle of the article, and i couldn't figure another better way than this, counting the article characters and split it at the middle.
The headings font name is "dirty headline", you can find it here
Thanks Mike!
Thanks for the reply and link to the FONT,.. it's really sharp looking and I like the way you sIFR'd it into your site.. great job!
Thanks again,
J
Excellent tip, however...
Most do not like to have the core codes of Joomla modified. Is there a joomla tutorial that shows how to do this without modifying the code? Come to think of it joomla 1.0v had a mambot that loaded modules into content mos=module or something like that.
Post new comment