Active Class on Blog-Menu and Blog-Posts URL

woods
Ssr. Bludit
Posts: 10
Joined: Mon May 25, 2020 9:56 pm

Ok so i checked it...

my blog link dont give me a slug (i guess because of the / at the end).

so i found a workaround for me, but i guess ist could be used in generell:

Code: Select all

					<?php foreach ($staticContent as $staticPage): ?>
						
						<?php $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>
						<?php $active = ($actual_link == $staticPage->permalink() or $actual_link == $staticPage->permalink() . '/')? 'active': ''; ?>
						<a class="<?php echo $active; ?>" href="<?php echo $staticPage->permalink(); ?>"><?php echo $staticPage->title(); ?></a>
		
					<?php endforeach ?>

First i check the actual link / url - http/https can both be set:

Code: Select all

<?php $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>
Then i set the var "active" if the actual link is identical with with permalink or with the permalink with an / at the end:

Code: Select all

<?php $active = ($actual_link == $staticPage->permalink() or $actual_link == $staticPage->permalink() . '/')? 'active': ''; ?>
I dont use php often, but it works and i understand what i did :D

So the main problem ist, that the slug didnt gave me something back, when i clicked on the "blog" link.
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

In the meantime it seems to work. :)
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
Post Reply