The Joomla! Forum ™



Forum rules


Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.



Post new topic Reply to topic  [ 38 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Mon Aug 25, 2008 7:46 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Mon Aug 25, 2008 7:31 pm
Posts: 3
If you want to assign different templates to different sections of your site, I made this code addition to the joomla core and it works nicely. (No more assigning templates to menu items). This way you can make a new content item under a certain section and it will automatically use the corresponding template (provided you put it in the right section), without assigning it a menu item in the template manager.

in includes/application.php look for this line: (line 308 in the getTemplate() function)
Code:
// Allows for overriding the active template from the request


After that line put the following:
Code:
$eItemId = JRequest::getVar('id');
$edb =& JFactory::getDBO();
$eQuery = 'SELECT sectionid as secID FROM #__content WHERE id LIKE '.$eItemId.'';
$edb->setQuery($eQuery, 0, 1);
$sectionId = $edb->loadResult();


Then under that you can do a simple conditional like so:
Code:
if ($sectionId == "5") {
$template = "template_name";
}

Just replace the number "5" with the ID of the section you want to assign the template to, and replace "template_name" with the folder name of the template you want to assign to that section.

All your additional non-default templates can be set to none on the menu item assignment list. Enjoy!


Top
 Profile  
 
PostPosted: Wed Aug 27, 2008 4:03 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri May 30, 2008 12:47 pm
Posts: 19
Location: New York, NY
Thanks I've been trying to figure out how to work around this for days :)


Top
 Profile  
 
PostPosted: Thu Aug 28, 2008 2:13 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri May 30, 2008 12:47 pm
Posts: 19
Location: New York, NY
Hi how would you go about adding an additional condition to this. Lets say to assign a different template to a category and not a section. Here is what I have based off of your code. I have been unsuccessful with it though.

$eItemId = JRequest::getVar('id');
$edb =& JFactory::getDBO();
$eQuery = 'SELECT categoryid as catID FROM #__content WHERE id LIKE '.$eItemId.'';
$edb->setQuery($eQuery, 0, 1);
$categoryId = $edb->loadResult();
if ($categoryId == "38") {
$template = "template_name";
}


Top
 Profile  
 
PostPosted: Thu Aug 28, 2008 2:46 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri May 30, 2008 12:47 pm
Posts: 19
Location: New York, NY
Nevermind fixed it with this code...

$eItemId = JRequest::getVar('id');
$edb =& JFactory::getDBO();
$eQuery = 'SELECT catid as catID FROM #__content WHERE id LIKE '.$eItemId.'';
$edb->setQuery($eQuery, 0, 1);
$catId = $edb->loadResult();
if ($catId == "38") {
$template = "template_name";
}


Top
 Profile  
 
PostPosted: Wed Oct 01, 2008 3:07 am 
Joomla! Intern
Joomla! Intern

Joined: Sun Jul 20, 2008 1:21 pm
Posts: 61
This topic was a godsend. I had a person that wants a webpage that basically has an aspect for the the frontpage but then changes to something completely different with the sections of the menu. I thought about applying the menu items to different templates but the problem was, he wants a module with news on the side (links to articles) and those would change the aspect of the page.
So I just thought I could not do it just by messing with the index.php and css. It became evident I was gonna have to hack the core somehow but how and where? I searched forever till I found this thread.

A godsend I tell you. Thanks for sharing.


Top
 Profile  
 
PostPosted: Fri Oct 17, 2008 7:59 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue May 02, 2006 3:18 am
Posts: 31
Thanks for the tip. This thread is indeed Godsend.

However, just wonderring, is there by any chance that we convert this so tht this can be configured through the administration panel instead (then saving it into say an xml file) rather than hard-coding the sectionid/catid into the .php file?

could anyone share some light on this?

Thanks :)


Top
 Profile  
 
PostPosted: Fri Oct 17, 2008 12:16 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Fri Oct 17, 2008 12:05 pm
Posts: 1
Location: San Francisco
Two things here:

First, and foremost, this is incredibly useful information, so much thanks for posting it. But, finding it took me absolutely forever. So, for the sake of the google searches for those who come after me, I'd just like to say: Set template by section. ;)

Secondly, the code as it stands doesn't work on the install I'm operating on. Some debugging turns up that $sectionId isn't actually getting assigned any value. So, for the moment, I'm using:
Code:
if ($eItemId == "23") {
     $template = "whatever";
     }


And it seems to be working. Can you explain to me why what seems to me to be the extra step of
Code:
 $edb->setQuery($eQuery, 0, 1);
$sectionId = $edb->loadResult();


and if my workaround is thus fragile in some way I'm not understanding?

Thanks again and in advance,
--esc


Top
 Profile  
 
PostPosted: Fri Oct 17, 2008 3:39 pm 
Joomla! Intern
Joomla! Intern

Joined: Tue Oct 14, 2008 1:30 pm
Posts: 53
Thank you everyone. This helped me so much! :D


Top
 Profile  
 
PostPosted: Tue Oct 21, 2008 2:03 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Tue Oct 21, 2008 1:59 pm
Posts: 1
hello all, i fllow this page , but for some reason i don't see any changes no error also new template also not changing

i am running Joomla 1.57

please help


Top
 Profile  
 
PostPosted: Tue Nov 25, 2008 8:23 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Mar 04, 2008 3:38 pm
Posts: 12
Thanks for a great solution. :D
I run 1.5.7 and got both the section version and the category version to work.
For those of you interested in working with different templates I would also like to point to this thread about assigning a template to a specific component:
http://forum.joomla.org/viewtopic.php?f=466&t=311714


Top
 Profile  
 
PostPosted: Wed Dec 10, 2008 3:17 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri Oct 17, 2008 5:17 pm
Posts: 6
I run 1.5.8 and section version does not work. Have any ideas?


Top
 Profile  
 
PostPosted: Wed Dec 10, 2008 3:51 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Wed Dec 10, 2008 2:41 pm
Posts: 1
Location: Buckinghamsire, England
Hello, i have been using joomla for about six months now and have become rather adjust to it.

however i have hit a wall.

below i have supplied two links to help explain what i require, for my site.



(the site has been configured with fire fox only at present with a 19" monitor.)


(this is my home page at current).
http://79.170.40.230/sohandy.co.uk/Joom ... alyst_j15/


(this is one of the other pages (articles))
http://79.170.40.230/sohandy.co.uk/Joom ... &Itemid=74


the second link is how i want all my pages other than the homepage to look. thats fine...

but the home page its self i am not happy with as i dont want it to display any white space at all. i do not want is to show any article space in other words.

only the wooden texture and the art work along with the horizontal menu and bouncy buttons.

can this be done without affecting the appearance of other pages.

Am i rite in thinking i will need to overide joomla!?

(the site has been configured with fire fox only at present with a 19" monitor as mentioned above.)

i would be so so greatfull of any help....

i thought this thread may be the related hope im rite.

regards,

Tom (new member)


Top
 Profile  
 
PostPosted: Wed Dec 10, 2008 10:51 pm 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Wed Feb 08, 2006 5:33 pm
Posts: 177
Location: amsterdam, holland
Maybe another way to target this problem is to do this with CSS using a unique id to the body tag. It doesn't involve core hacks: viewtopic.php?f=127&t=281999

_________________
http://www.dwarshuis.com


Top
 Profile  
 
PostPosted: Wed Dec 10, 2008 11:33 pm 
User avatar
Joomla! Virtuoso
Joomla! Virtuoso

Joined: Fri Dec 21, 2007 9:36 pm
Posts: 3135
Location: Yorkshire, UK
I have not personally tried the techniques presented but I reckon that they may become useful at some point. Cheers for posting :).

Regards

_________________
Freedom of expression ... some may try to suppress it but they can never take it away ...
There is no problem a good miracle can't fix.


Top
 Profile  
 
PostPosted: Thu Dec 11, 2008 2:35 pm 
Joomla! Intern
Joomla! Intern

Joined: Tue Oct 14, 2008 1:30 pm
Posts: 53
I have been using this mod successfully- but now I am stuck in a different situation.

I have three sections- I want two of them to use template A and one of them to use template B.

I can't figure out how to get this to assign TWO sections to the template...

Any suggestions? Please?


Top
 Profile  
 
PostPosted: Thu Dec 11, 2008 2:44 pm 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Wed Feb 08, 2006 5:33 pm
Posts: 177
Location: amsterdam, holland
remember in the joomla backend you can assign _menu items_ to a template, as far as I know, not sections. If you want to do that, I would try the css solutions I mentioned

_________________
http://www.dwarshuis.com


Top
 Profile  
 
PostPosted: Thu Dec 11, 2008 3:33 pm 
Joomla! Intern
Joomla! Intern

Joined: Tue Oct 14, 2008 1:30 pm
Posts: 53
Where does the code go? In index.php of the default template- or the one I want to assign to the menu item?

Also I'm not clear about what is set up within the menu parameters to make this work- do you put in the template name "page class suffix" in the system parameters?


Top
 Profile  
 
PostPosted: Thu Dec 11, 2008 3:50 pm 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Wed Feb 08, 2006 5:33 pm
Posts: 177
Location: amsterdam, holland
Of course I am not sure what your specific set up is, but the idea is to replace in index.php the body tag with the code provided. Next step is to handle everything using css. So you need to know about how css works.

The menu parameters give you extra ways to assign a css class or id to the html tags.

_________________
http://www.dwarshuis.com


Top
 Profile  
 
PostPosted: Mon Dec 15, 2008 9:53 am 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue May 08, 2007 1:49 pm
Posts: 22
This works perfectly for me in 1.5.8 using:
Code:
if ($sectionId == "1") {
$template = "template_one";
}


but, I have a few different sections that I'd like to assign different templates to, so I tried this:
Code:
if ($sectionId == "1") {
$template = "template_one";
} else if ($sectionId == "2") {
$template = "template_two";
}


Navigating to section1 works fine, but in section2 the default template (let's call it template_main) is applied...

Any help will be much appreiciated.


Top
 Profile  
 
PostPosted: Mon Dec 15, 2008 5:00 pm 
Joomla! Intern
Joomla! Intern

Joined: Tue Oct 14, 2008 1:30 pm
Posts: 53
DaBouncer wrote:
This works perfectly for me in 1.5.8 using:
Code:
if ($sectionId == "1") {
$template = "template_one";
}


but, I have a few different sections that I'd like to assign different templates to, so I tried this:
Code:
if ($sectionId == "1") {
$template = "template_one";
} else if ($sectionId == "2") {
$template = "template_two";
}


Navigating to section1 works fine, but in section2 the default template (let's call it template_main) is applied...

Any help will be much appreiciated.


This is the the same problem I am having- let us know if you solve it and I will do the same.


Top
 Profile  
 
PostPosted: Tue Dec 16, 2008 6:01 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue May 08, 2007 1:49 pm
Posts: 22
This will load a named template for all sections (except index.php):
Code:
      if ($sectionId < "1") {
      $template = "template_one";
      } else {
      $template = "template_two";
}


But I find it strange that this does not work:

Code:
      if ($sectionId == "1") {
      $template = "template_one";
      } elseif ($sectionId == "2") {
      $template = "template_two";
      } elseif ($sectionId == "3") {
      $template = "template_three";

}


:eek:


Top
 Profile  
 
PostPosted: Sat Jan 17, 2009 10:05 pm 
Joomla! Ace
Joomla! Ace

Joined: Wed Jan 18, 2006 1:28 pm
Posts: 1149
Location: West Midlands, UK
This looks like a very handy trick, but I only get a partial success.

In the section I am using I have some content items entered via the back end and some entered via ChronForms SubmitContent form.

The template gets assigned correctly for the items entered from ChronoForms, the others get the default template.

The difference in the data seems to be that ChronoForms doesnt set the values of the fields Metadata, Attribs, Created_by or Alias.

If a page is edited, these fields get their default value and the trick stops working.

_________________
http://www.blackwellcomputing.co.uk


Top
 Profile  
 
PostPosted: Tue Jan 20, 2009 7:44 am 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Wed Sep 27, 2006 1:10 am
Posts: 207
Location: Sterling, VA, USA
DaBouncer wrote:
This will load a named template for all sections (except index.php):
Code:
      if ($sectionId < "1") {
      $template = "template_one";
      } else {
      $template = "template_two";
}


But I find it strange that this does not work:

Code:
      if ($sectionId == "1") {
      $template = "template_one";
      } elseif ($sectionId == "2") {
      $template = "template_two";
      } elseif ($sectionId == "3") {
      $template = "template_three";

}


:eek:


What happens if you use all ifs and no elseifs? There is only ever going to be one sectionid set at a time.
So try:
Code:
if ($sectionId == "1") {
      $template = "template_one";
      }
if ($sectionId == "2") {
      $template = "template_two";
      }
if ($sectionId == "3") {
      $template = "template_three";

Not personally tested, but that's what I' try.

_________________
Plethora Design - http://www.plethoradesign.com.
Joomla extensions - http://www.plethoradesign.com/downloads.


Top
 Profile  
 
PostPosted: Tue Jan 20, 2009 7:54 am 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Wed Sep 27, 2006 1:10 am
Posts: 207
Location: Sterling, VA, USA
sure would be nice to have a way to assign a template to a section or category by default, and have that overriden if a template's assigned to a menu item (for example to an Article Layout).
Let's say you create a menu item pointing to a Section or Category layout, then you can assign a template to that. That can of course be in a hidden menu. In such a case, it would seem logical that all items belonging to that section or category have that template assigned to them UNLESS they are already assigned a different one.

_________________
Plethora Design - http://www.plethoradesign.com.
Joomla extensions - http://www.plethoradesign.com/downloads.


Top
 Profile  
 
 Post subject: Search engine friendly?
PostPosted: Mon Mar 16, 2009 6:52 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Mar 08, 2007 10:46 am
Posts: 10
Hi,

Thanks for the code. I am using Joomla 1.5.9 and it worked for me just fine.

One question though, can this be done so that you can use Search Engine Friendly URL's? When I clicked on that, then the templates disappeared, but it would be nice to be able to use SEF url's.


Top
 Profile  
 
PostPosted: Mon Mar 16, 2009 8:56 pm 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Wed Sep 27, 2006 1:10 am
Posts: 207
Location: Sterling, VA, USA
Hi Meekochan,

Please post the code you used so we can understand what exactly worked for you, and what you were trying to do. Some of us wanted to be able to assign a template by default to a section and have it assigned to all related articles in that section automatically. I don't think that is what you are talking about.

Did you use if/else/else or if/if/if as I suggested? It would be good to know what code you used, since I have not tried my suggested fix myself.

Thanks,
Casper

_________________
Plethora Design - http://www.plethoradesign.com.
Joomla extensions - http://www.plethoradesign.com/downloads.


Top
 Profile  
 
PostPosted: Mon Mar 16, 2009 9:01 pm 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Wed Sep 27, 2006 1:10 am
Posts: 207
Location: Sterling, VA, USA
For making this work with search engine friendly URLs (SEF), I think the code needs to be altered so that:

Code:
$sectionId


becomes :

Code:
$_REQUEST['sectionId']


That would be what I'd try first.

So the code would be:
Code:
if ($_REQUEST['sectionId'] == "1") {
$template = "template_one";
}
if ($_REQUEST['sectionId'] == "2") {
$template = "template_two";
}


and so on.

_________________
Plethora Design - http://www.plethoradesign.com.
Joomla extensions - http://www.plethoradesign.com/downloads.


Top
 Profile  
 
PostPosted: Tue Mar 17, 2009 8:29 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Mar 08, 2007 10:46 am
Posts: 10
Code:
$eItemId = JRequest::getVar('id');
$edb =& JFactory::getDBO();
$eQuery = 'SELECT sectionid as secID FROM #__content WHERE id LIKE '.$eItemId.'';
$edb->setQuery($eQuery, 0, 1);
$sectionId = $edb->loadResult();

if ($sectionId == "1") {
$template = "siteground-j15-23";
}

if ($sectionId == "2") {
$template = "siteground-j15-65";
}

if ($sectionId == "3") {
$template = "siteground-j15-41";
}

if ($sectionId == "4") {
$template = "siteground-j15-79";
}

if ($sectionId == "5") {
$template = "siteground-j15-71";
}

if ($sectionId == "6") {
$template = "siteground-j15-76";
}

if ($sectionId == "7") {
$template = "siteground-j15-48";
}


This is what I wrote in. I think you're right. I thought it worked, because when I made a menu link to an article, the template changed. But when I click on an article from a Menu type Articles Section Blog Layout, then the template doesn't change. Is this the problem others have been having?

Actually, when I click on the Menu that leads me to the Articles Section Blog Layout, I don't get the template change either.

And since the article title is linkable on my page, even though the template changes when I get to an article by clicking on the menu link to it, once I click on the article title, I get the default template again. Hmm.

Sadly, your solution for the SEF url's did not work for me. Everything just had the default template.


Top
 Profile  
 
PostPosted: Fri Jun 26, 2009 4:55 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Oct 13, 2005 10:36 pm
Posts: 30
This isn't working for me in 1.5.1.1. Here's what I have in application.php:

$eItemId = JRequest::getVar('id');
$edb =& JFactory::getDBO();
$eQuery = 'SELECT sectionid as secID FROM #__content WHERE id LIKE '.$eItemId.'';
$edb->setQuery($eQuery, 0, 1);
$sectionId = $edb->loadResult();

//this assigns the home page template to the Promotions section
if ($sectionId == "11") {
$template = "theme213";
}
Is there something wrong with my code?


Top
 Profile  
 
PostPosted: Fri Jun 26, 2009 5:35 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Oct 13, 2005 10:36 pm
Posts: 30
My fault - please disregard my previous post, this is working perfectly.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 38 posts ]  Go to page 1, 2  Next



Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group