Categories
pCloud Premium

Joomla useful PHP conditions to create your template

Joomla useful PHP conditions to create your template

If you want to load a module, script, css file, text or image in a specific location in your website, here you'll find how to do it. With the help of php we can show specific content in a specific locations like: Article ID, Section ID, Category ID, Search page, Menu ID, Virtuemart pages etc.


Root directorie

This code is calling your root folder of your Joomla installation <?php echo $this->baseurl ?>

Page Options

  1. <?php $pageoption1 = JRequest::getVar( 'Itemid', '' ); $pageoption2 = JRequest::getVar( 'page', '' ); $pageoption3 = JRequest::getVar( 'option', '' ); ?>

With this code we assign three definitions: $pageoption1, $pageoption2 и $pageoption3

To use them in our template, we'll have to insert the code in the head section of the page.

Then if we want to show an element in a specific Menu ID, we write:

$pageoption1

Example link: index.php?option=com_content&view=article&id=34&Itemid=1&lang=bg

  1. <?php if ($pageoption1 == '1') { ?>
  2. This text will be visible only in the page with Menu Item ID 1
  3. <?php } ?>
  1. <?php if (($pageoption1 == '1') OR (($pageoption1 == '2')) OR (($pageoption1 == '3')) OR (($pageoption1 == '12'))) { ?>
  2. This text will be visible only in menus with ID 1,2,3 и 12
  3. <?php } ?>
  1. <?php if ($pageoption1 == '1')
  2. {echo '<div id="my_div_one"></div>'; } 
    else { echo '<div id="my_div_two"></div>'; }
  3. ?>

If we are in menu ID 1, then we'll see this <div id="my_div_one"></div>

In any other menu we'll see this one <div id="my_div_two"></div>

$pageoption2

Example link: index.php?page=shop.product_details&category_id=1&flypage=flypage.tpl&product_id=1&option=com_virtuemart&Itemid=1

  1. <?php if ($pageoption2 == 'shop.product_details') { ?>
  2. This text will be visible in product page in Virtuemart
  3. <?php } ?>
  1. <?php if (($pageoption2 == 'shop.cart') OR (($pageoption2 == 'checkout.index')) OR (($pageoption2 == 'checkout.thankyou'))) { ?>
  2. This text will be visible in 3 specific Virtuemart pages: "The Cart", "Checkout" and "Thank you page".
  3. <?php } ?>

$pageoption3

Example link: index.php?option=com_content&view=article&id=1&Itemid=1&lang=bg

  1. <?php if ($pageoption3 == 'com_content') { ?>
  2. This text will be visible only if we are browsing a Joomla! content.
  3. <?php } ?>

Home Page

Joomla is forcing us to mark one of the menus as the Default menu, in other words our home page.

In order this to work we must place this code in the head section of the page:

  1. <?php
  2. $defaultMenu = JFactory::getApplication()->getMenu()->getDefault()->id;
  3. $currentMenu = JFactory::getApplication()->getMenu()->getActive()->id;
  4. if($defaultMenu == $currentMenu){
  5. $homePage = true;
  6. }else{
  7. $homePage = false;
  8. }
  9. ?>

Now we can use this:

  1. <?php if($homePage == true): ?>
  2. This text will be visible ony on the home (Default) page
  3. <?php endif; ?>

Or that:

  1. <?php
  2. if ($homePage == true) {
  3. echo "If we are in the home page, this text will be visible";
  4. } elseif ($homePage == false) {
  5. echo "In any other case, it'll be this one";
  6. }
  7. ?>

Languages

Example link: index.php?option=com_content&view=article&id=1&Itemid=1&lang=bg

  1. <?php if($this->language == "bg-bg") { ?>
  2. This text will be visible only if the site language is bulgarian
  3. <?php } ?>
  1. <?php if($this->language == "en-gb") { ?>
  2. This text will be visible only if the site language is english
  3. <?php } ?>

We can combine any of the previous codes, for example:

  1. <?php if ($pageoption1 == '1') { ?>
  2. <?php if($this->language == "bg-bg") { ?>
  3. This text will be visible only if the menu has ID 1 and the current language is bulgarian
  4. <?php } ?>
  5. <?php } ?>

Joomla articles, sections, categoryes & others

  1. <?php if (JRequest::getVar('view')=='article') { ?>
  2. This text will be visible only if we are browsing an article
  3. <?php } ?>
  1. <?php if (JRequest::getVar('view')=='frontpage') { ?>
  2. This text will be visible only in the FrontPage section
  3. <?php } ?>
  1. <?php if (JRequest::getVar('view')=='section') { ?>
  2. This text will be visible only if we are browsing a Section layout
  3. <?php } ?>
  1. <?php if (JRequest::getVar('view')=='section' && JRequest::getVar('id')==1) { ?>
  2. This text will be visible only if we are browsing Section with a specific ID
  3. <?php } ?>

If you have suggestions, how to make this article more useful, please leave a comment.

I hope that this article will help you. ico

<?php
// This is the content of index.php in the root of your template directory
defined( '_JEXEC' ) or die( 'Restricted access' );
 
$CssDir = $this->baseurl . '/templates/templatename/css';
 
$defaultMenu = JFactory::getApplication()->getMenu()->getDefault()->id;
$currentMenu = JFactory::getApplication()->getMenu()->getActive()->id;
 
if($defaultMenu == $currentMenu){ 
   $bodyId = 'id="home"'; 
   $homePage = true; 
}else{
   $bodyId = ''; 
   $homePage = false; 
}
 
?>
27 March 2012г.
Viewed 354823 times