Hi all,
Is there a way to add distinct classes to the <body>
tag depending on the page being viewed? This would be extremely useful for theming purposes.
Any information will be greatly appreciated.
Hi all,
Is there a way to add distinct classes to the <body>
tag depending on the page being viewed? This would be extremely useful for theming purposes.
Any information will be greatly appreciated.
OK, I was able to solve this with JQuery.
add-body-classes.js
$(document).ready(function() {
if(location.search == ââ){
$(â.fixedâ).addClass(âinitialâ);
}
if(location.search == â?page=homeâ){
$(â.loginâ).addClass(âloginpageâ);
}
if(location.search == â?p=subscribeâ){
$(â.fixedâ).addClass(âsubscribeâ);
}
});
Can you explain further please, so as to offer help to any one else who would like to do this? Cheers.
Hereâs an improved version of the script.
The JavaScript searches for a specific string in the URL and, when a match is found, it will append an additional class attribute that we define to the <body>
tag. Itâs not fully automatic since we still have to tell the script what string to find in the URL and also what class to append.
$(document).ready(function() { // This executes the script once the page is loaded.
if(location.search == ""){ // If the URL of the page is the root and does not have a path string, then...
$(document.body).addClass("initial"); // Append the 'initial' class to the `<body>` tag.
}
if(location.search == "?page=home"){ // If the URL contains the string '?page=home', then
$(document.body).addClass("loginpage"); // Append the 'loginpage' class to the `<body>` tag.
}
if(location.search == "?p=subscribe"){ // If the URL contains the string '?page=subscribe', then
$(document.body).addClass("subscribe"); // Append the 'subscribe' class to the `<body>` tag.
}
});
The script was saved as âadd-body-class.jsâ and placed in the admin/js directory. Then I added the following line via the administration settings:
Config -> Settings -> subscription-ui settings -> Footer of public pages
<script type="text/javascript" src="admin/js/add-body-class.js"></script>
</body>
One thing that would help make the script automatic would be to âcleanâ up the URL and remove the query string that is generated on the URL. Drupal and WordPress use the htaccess file to create some server-side rewrites and remove the â?p=â variables. They call it âClean URLsâ or âPermalinksâ.
It would be mighty nice if phpList could do the same trick.
You mean for public pages (subscription pages) or admin pages? If admin pages, what kind of theming are you doing there? It is probably easier to create a new phpList theme based on Trevelin (aka Bootlist).
Absolutely; that is supported in phpList 4 but not 3.
Thank you Sam for the response. I mean only to theme the public facing pages, not the admin or internal pages. That is not really necessary since the current administration theme is quite visually awesome. However, much more flexibility is needed for the public front end and subscriber pages.
By the way I tried tackling the problem with URLs (with the mighty help at Stackoverflow) and got some weird problems. But Iâm going to open a new thread.
Just to clarify the purpose of having body classes for theming purposes. We turned the mundane links into classy buttons, which only affect this page.
Before:
Wow, nice. Thatâs a much clearer use case, thanks.