Joomla 1.5

Joomla 1.5 - add a new module position inside an article

, on May 11, 2008

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.

Anonymous's picture
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.

Mihai's picture
Thanks, i am glad it helped. For your question. You can use the <?php $articletitle ?> variable. Something like this: <?php 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.

Anonymous's picture
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.

Anonymous's picture
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.

Mihai's picture
Thanks for the info Rob, i didn't know that. It seems to be the fastest way to create a new position. Again, thanks.

Anonymous's picture
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??

Anonymous's picture
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.

Anonymous's picture
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

Mihai's picture
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

Anonymous's picture
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

Anonymous's picture
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.

Anonymous's picture
I need to place an outside web page inside the jooomla script, where I should see this page showing insdie my webpages, not just a small module like the one shown above but a complete page

Anonymous's picture
heh this is cool i spent many time to tinking how to do this thanks for the info

Anonymous's picture
Well, I found your article very usefull, somentimes more then tag {loadposition xyz}, infact you can manage modules display in that position via menu for different articles and I think is good. Starting from your code I would like to add one new position after the first image in article...considering that I'm new to php which function can you suggest to point me to the right direction? Thanks for sharing this article.

Anonymous's picture
i try the same thing for 2 days now

Anonymous's picture
Nouri S. Kelani wanted to add an external webpage inside Joomla. Unless I'm missing something he wanted, the "wrapper" under 1.5 does this nicely.

Anonymous's picture
Exactly what I was looking for! But is there a way to limit the module insertion to only the first article. Tried a bunch of things with no luck.

Anonymous's picture
TY. All night i was trying to do something like that and thanks to you i made it in 5 minutes.

Anonymous's picture
I'm trying to make this work but it isn't. I did replace your_module_position with the position I created. Where do you use firstpart and where do you use second. would the whole code look like this? <?php $article = $this->article->text; $countchars = strlen($article); $divide = $countchars / 2; $firstpart = substr($article, 0, $divide); $secondpart = substr($article, $divide); ?> <?php echo $firstpart; ?>
<?php $myblurb_modules = &JModuleHelper::getModules( 'actual_module_position' ); foreach ($myblurb_modules as $myblurb) { $_options = array( 'style' => 'xhtml' ); echo JModuleHelper::renderModule( $myblurb, $_options ); } ?>
<?php echo $secondpart; ?> Of course it's not going to show right here but you get the idea.

Mihai's picture
It seems you're doing it right. You have to remove the < ? php echo $this->article->text; ? > line thou. Does the system prints an error, or in what way it does not work ?

Anonymous's picture
No it doesn't show any errors only does not show the module inside the text. I also removed the line you suggested still nothing. Does this have to do with which template we use at all? Also I am trying this on local server wamp, I don't know if that does anything to it.

Mihai's picture
You have to edit templateDetails.xml in your current template and define the new position actual_module_position. You can try to create a simple HTML module (with a line of text inside or something like this) and print it in that position. Adsense takes some time until it is shown on the page and i think on localhost it takes even longer or it doesn't show at all.

Anonymous's picture
I did create the position inside my template's templatesetails.XML That's the way I'm trying it with some text it works if I insert {loadposition xyz} inside the article itself. Anyway it seems like it's working for many people I'll try it again at a different site tonight. Thanks

I tried it with and without the line < ? php echo $this->article->text; ? > this code is really showing messed up here but in a simple way 1 countcharacters 2 firstpart 3 moduleposition 4 secondpart
Anonymous's picture
I must be doing something wrong can't figure it out though. Here's what I'm doing maybe someone can see if i am making a mistake. First i am adding a module position in the default template's templateDetails.xml then i created a custom html module and publish it to the new position i added. Then i went to components/com_content/views/article/tmpl/default.php and added after
<?php if (isset ($this->article->toc)) : ?> <?php echo $this->article->toc; ?> <?php endif; ?> The code <?php $countchars = strlen($article); $divide = $countchars / 2; $firstpart = substr($article, 0, $divide); $secondpart = substr($article, $divide); ?> <?php echo $firstpart; ?>
<?php $myblurb_modules = &JModuleHelper::getModules( 'actual_module_position' ); foreach ($myblurb_modules as $myblurb) { $_options = array( 'style' => 'xhtml' ); echo JModuleHelper::renderModule( $myblurb, $_options ); } ?>
<?php echo $secondpart; ?> the next line below this code would be<?php echo $this->article->text; ?>

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.