DeeEmm

Pragmatism in code

Tested on Joomla Version 1.6

If you are using a custom template and want to control how it is displayed, you may need the following mod.

This quick easy modification will allow you to only show some page elements to guests. I made this modification to hide the slideshow in Gobbers Themes 'Comenian' template, but it can easily be adapted for other elements on other templates.

Basically we need to wrap the element we want to hide in some php code that checks if the user is logged in. If they are not, then the element is shown.

Open the templates main index file, you can find this in the template directory, in this case it is

/templates/comenian/index.php

The index.php file is the file that generates the main template layout, simply browse to the template you wish to alter.

Open index.php in your favourite editor, and find the element you wish to hide, in this case it is the slideshow. It can be any piece of code in the template. A tip here is to use your browser and the view code / DOM inspector (such as firebug) to find the code you wish to hide, and then look for it in the index.php file.

Here's the code we wish to hide for the comenian template

<div id="slideshow-w">
 <div id="slideshow">
 <img src="/templates/<?php echo $this->template ?>/images/slide1.jpg" alt="image1" />
<img src="/templates/<?php echo $this->template ?>/images/slide2.jpg" alt="image2" />
 <img src="/templates/<?php echo $this->template ?>/images/slide3.jpg" alt="image3" />
 <img src="/templates/<?php echo $this->template ?>/images/slide4.jpg" alt="image4" />
 <img src="/templates/<?php echo $this->template ?>/images/slide5.jpg" alt="image5" />
</div>
</div>
<script type="text/javascript" charset="utf-8">
var $j = jQuery.noConflict();
$j(document).ready(function(){
 $j("#slideshow").slideshow({
 pauseSeconds:<?php echo $pause ?>,// 5,
 height:<?php echo $hauteur ?>, //469,
 fadeSpeed:<?php echo $fadespeed ?>,// 0.5,
width:<?php echo $largeur ?>, //950,
 caption: false
 });
 });
 </script>

Insert the following code before the elements you wish to hide

<?php 
$user = JFactory::getUser();
if($user->guest ) : ?>

The first line is a class call that loads the class required to generate the variables we need to check. The second line is the check itself.

Now insert the following code after the elements you wish to hide

<?php endif; ?>

The two commands form a basic if statement, if the statement is true, the code in between will be executed.

To make the code only execute if the statement is false - ie only display for logged in users, simply add an exclamation mark before the logic like this...

if(!$user->guest ) : ?>

You can also test for other conditions here as well, such as only displaying the code on specific pages. To find out how to do this, see this article