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



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.
i also want to add new module inside article. but this is not working.
can u help me please??
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.
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.
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 for the info Rob, i didn't know that. It seems to be the fastest way to create a new position.
Again, thanks.
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??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 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 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 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
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.
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
heh this is cool i spent many time to tinking how to do this
thanks for the info
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.
i try the same thing for 2 days now
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.
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.
TY. All night i was trying to do something like that and thanks to you i made it in 5 minutes.
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; ?>
$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.
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 ?
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.
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.
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 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 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; ?>
$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; ?>
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
This is perfect and easy to implement. But only one thing is here that is not understood .
Why the component directory is used for creating the new position?
hi
i am also doing same thing in joomla which u have done but can't figure out what i have done wrong can u help me
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; ?>
Thanks guy! This is really helpful post as well as comments. I got very useful and informative information. Thanks again for sharing.
Great trick, it is awesome!
Is there any way to count characters and display the module after the first 200 characters?
You can replace this part:
$divide = $countchars / 2;
$firstpart = substr($article, 0, $divide);
$secondpart = substr($article, $divide);
with this one:
$countFirstPart = 200;
$countSecondPart = $countchars - $countFirstPart;
$firstpart = substr($article, 0, $countFirstPart );
$secondpart = substr($article, $countSecondPart );
also when there is a pagebreak it displays the article twice in the same page
Thank you very much, however there seem to be a problem sometimes it gives the first 200 characters (or first part if divided in half) and then for the secondpart it starts from the top I made a screenshot because this is not live. http://farm6.static.flickr.com/5083/5209888110_0acf2923e3_b.jpg
If it helps it seems to do this mostly when there are pagebreak titles or a prev-next button at the end of the article.
Any suggestions to solve this problem?
I missed to include this in the article but you have to remove the
< ? php echo $this->article->text; ? >
line or the article will print 2 times
Thank you for your answer, but as I can see the main problem is that adsense module makes invalid the article code, for example inserts inside img tag ()
for example inserts inside img tag (<_img [here goes adsens module display] src"..."/>)
for example inserts inside img tag (<_img [here goes adsens module display] src"..."/>)
This searches for the first tag after the middle of the article and re split the article there so you won't be inside a tag .
If you don't use br's in your articles, modify that line and add what tag you want (paragraphs maybe)
Replace the code with this one :
<?php
$article = $this->article->text;
$countchars = strlen($article);
$divide = $countchars / 2;
$firstpart = substr($article, 0, $divide);
$secondpart = substr($article, $divide);
// change the tag in this line
$findTag = strpos($secondpart, '< b r / >');
$var = $divide + $findTag;
$firstpart = substr($article, 0, $var);
$secondpart = substr($article, $var);
?>
Thanks, it helps me a lot!
Hi. How can I put the module position at the and of article and before plugin position? Thank you for answer!!!
i install article into module plg and publish in insert article {module [230]}
but not showing module
i dont want to split the artilce content, I just want the position before or after the article
so i did it like this
<?php if (isset ($this->article->toc)) :
echo $this->article->toc; ?>
<?php endif; ?>
<?php echo $this->article->text; ?>
<?php
$myblurb_modules = &JModuleHelper::getModules( 'userarea' );
foreach ($myblurb_modules as $myblurb) {
$_options = array( 'style' => 'xhtml' );
echo JModuleHelper::renderModule( $myblurb, $_options );
}
?>
But its is not displaying modules... :(
Try to modify the file in
/{joomla_install}/templates/{your_theme}/html/com_content/views/article/tmpl/default.php
incredable ! i do it ! thansk !
Post new comment