louisstephens Posted February 18, 2016 Share Posted February 18, 2016 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 More sharing options...
kongondo Posted February 18, 2016 Share Posted February 18, 2016 (edited) Yeah, you do need to read up on get() and find() and the lot... What you statement is saying is this: Go and get the page 'url-one' If you got that page, echo one.css 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 February 18, 2016 by kongondo 2 Link to comment Share on other sites More sharing options...
louisstephens Posted February 18, 2016 Author Share Posted February 18, 2016 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). 1 Link to comment Share on other sites More sharing options...
Jan Romero Posted February 19, 2016 Share Posted February 19, 2016 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/". 2 Link to comment Share on other sites More sharing options...
pwired Posted February 19, 2016 Share Posted February 19, 2016 Just set a default css in _init.php, then use the template file of a page to override the default value. If you want to use compare, using page id would be more easy. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now