Jump to content

Change stylesheet based On URL


louisstephens
 Share

Recommended Posts

I thought I had this, but I guess my understanding of get/find for pages is a bit lacking (or perhaps nonexistent). I am trying to switch the stylesheet based on the URL using a simple if/else statement, but what I wrote doesn't seem to be doing the job.

I currently just have two pages (that each need two different background images), but they share the same head.inc file. I wrote:

<?php
// Switch stylsheets based on site
$styleSwitcher = $pages->get('/url-one/');

	if($styleSwitcher){
		echo "<link rel='stylesheet' href='{$config->urls->templates}css/one.css' />";
	}

	else{
		echo "<link rel='stylesheet' href='{$config->urls->templates}css/two.css' />";
	}



?>

I assume I messed up my "get" as both pages are still loading one.css

Link to comment
Share on other sites

Yeah, you do need to read up on get() and find() and the lot... :-)

What you statement is saying is this:

  1. Go and get the page 'url-one'
  2. If you got that page, echo one.css
  3. Else (if you didn't get the page 'url-one') echo two.css

Pausing for a moment, do you see where your mistake is? :-)...As long as you have a page called 'url-one', $styleSwitcher will always be true irrespective of which page is currently being viewed and one.css will always get loaded. What you really want is to check if the current page (being viewed) has a url that is equal to the page 'url-one' URL. You haven't done that in your conditionals/logic. That's what you are missing.

Quick tip: sometimes it is best to write down the logic you are after in 'plain language/word form' and the translate that to code. 

OK, I think I've given you enough hints to get you going....I'm trying to help you to learn. If you get stuck, let us know and we'll help :-)

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

Kongondo, I had a d'oh moment, but I appreciate the quick response. I really felt dumb there ha ha  <_<  . I realized I wasn't actually comparing it to anything so I changed it to:

<?php
$styleSwitcher = $pages->get('/url-one/');
$baseURL = $page->url;

  if($styleSwitcher == $baseURL){
    echo "<link rel='stylesheet' href='{$config->urls->templates}css/one.css' />";
}
  else{
    echo "<link rel='stylesheet' href='{$config->urls->templates}css/two.css' />";
}

?>

And with that, problem solved. I actually think I understand the find/get for $pages a lot better as well (did a lot more reading).

  • Like 1
Link to comment
Share on other sites

You don’t actually need to load the page at /url-one/, since you already know its url, and that’s all you need for your switch logic.

Also, your variable $styleSwitcher refers to a Page object, but you’re comparing it to a string (the current page’s url).

I would suggest simply comparing $page->url to "/url-one/".

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...