building the site Conditional block using PHP
I have been struggling a bit about the header blocks since there are some template differences between pages, databases and applications. Now I think i have a solution for it however as I have tested to use raw PHP in the blocks. This allow me to display content based on the URL, which seem to be working fine.
Adding a bit of PHP for the blocks was not really very difficult. There are plenty of examples online and I started with a simple url match using the $_SERVER['REQUEST_URI'] function.
$host = $_SERVER['REQUEST_URI'];
if($host == '/projects/')
{
echo ('Whatever you want in here');
}
else
{
echo('Whatever you want in here or leave blank to show nothing');
}
I did however need to change this a bit since there are pagination and I want to have the same content on all pages for the pagination. It's easy since there is just a number that is added to the URL for each page. I should make a proper solution for this, but for now I just added the pagination pages in an array. I also tested to include other blocks, which worked fine. This should make things a bit more dynamic if I want that.
$host = $_SERVER['REQUEST_URI'];
if(in_array($host ,array(
"/projects/",
"/projects/page/2/",
"/projects/page/3/",
"/projects/page/4/",
"/projects/page/5/"
))
)
{
echo ('whatever you want here');
}
elseif ($host == '/projects/projektledare/') {
echo('
{block="gallery_hero"}
');
}
else
{
echo('');
}
Overall this solve the immediate issue and I can proceed as planned. I just want to make sure I can get the pagination working without manually adding all the pages. That is something to look for tomorrow however as today I am going to sleep and enjoy the progress so far.
0 Comments
Recommended Comments
There are no comments to display.
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