Help - Search - Members - Calendar
Full Version: Showing the active link with php
Movable Type Community Forum > Additional Resources > Tips and Tricks
dotpitch
On top of the page I have a list of the categories. When clicking on one of those labels you're going to the first entrypage within that category which has an identical layout as the first (index)page. The only difference is a submenu on the left with a list of the entries in that category (which all link to those individual pages etc.)
But I want the active category to have a different linkcolor than the others. Normally this could be done by javascript (which I'm not familiar with), but could it also be done this way?:

[The links on top of the first page]
CODE
[URL=<$MTCategoryArchiveLink$>?activecat=<$MTCategoryLabel$>]</a>


[The links on top of the other pages ones one has made a choice]
CODE
<MTCategories>
<?php
if($activecat==<$MTCategoryLabel$> {
<a href="<$MTCategoryArchiveLink$>" class="active"><$MTCategoryLabel$>[/URL]
} else {
<a href="<$MTCategoryArchiveLink$>"><$MTCategoryLabel$></a>
?>
</MTCategories>



I don't know if php will work this way, because MT already filled in the tags.
kadyellebee
Well, that might work. But your php needs some tweaking, I'd bet...

CODE
<?
$activecat="<$MTArchiveCategory$>";
?>
<MTCategories>
<?php
if($activecat=="<$MTCategoryLabel$>") {
echo "<a href=\"<$MTCategoryArchiveLink$>\" class=\"active\"><$MTCategoryLabel$></a>";
} else {
echo "<a href=\"<$MTCategoryArchiveLink$>\"><$MTCategoryLabel$></a>";
?>
</MTCategories>


I'm assuming this will be going on a category archive page, so you'd set it up so the MTArchiveCategory is what the php test is comparing. And plus, I escaped your quotation marks. smile.gif

Kristine
superblue
This sounds like something I'd like to do, but I'm having a bit of trouble getting it working... Primarily I get a php error, but the line number it references is greater than the number of lines in the file containing the code... why's that?

Just wondering if somebody has this successfully working? At the moment I'm using the "ifcategory" plugin to decide which from a range of navigation bars to include via php. So a php file with "diary" set as the active colour is inserted into all pages in the diary category, whilst the other category links in that php file are coloured as links.

The problem with this is there's a lot of hassle when I add new categories, and lots of external files being included. The above post sounds to me like the answer... just not sure how to implement it... Any input would be very welcome!
kristine
Looks like when I wrote that code, I forgot an ending } curly bracket before the ?> That would make the php error show up at the end of the document because it didn't find the end } curly bracket to close the statement. smile.gif

Hope that helps,
Kristine
superblue
Thanks Kristine, that's got it working! biggrin.gif

Am trying to get it working on individual archive pages too now, got a bit of tweaking to do I think...
superblue
For anyone interested, here's the code I'm using for showing active links on both individual and category archive pages:

individual:

CODE
<?
$activecat="<$MTEntryCategory$>";
?>
<MTCategories show_empty="1">
<?php
if($activecat=="<$MTCategoryLabel$>") {
echo "<a class=\"sub_menu_active\" title=\"<MTCategoryDescription>\" href=\"<$MTCategoryArchiveLink$>\"><$MTCategoryLabel$></a><br>";
} else {
echo "<a class=\"sub_menu\" title=\"<MTCategoryDescription>\" href=\"<$MTCategoryArchiveLink$>\"><$MTCategoryLabel$></a><br>"; }
?>
</MTCategories>


and category:

CODE
<?
$activecat="<$MTArchiveCategory$>";
?>
<MTCategories show_empty="1">
<?php
if($activecat=="<$MTCategoryLabel$>") {
echo "<a class=\"sub_menu_active\" title=\"<MTCategoryDescription>\" href=\"<$MTCategoryArchiveLink$>\"><$MTCategoryLabel$></a><br>";
} else {
echo "<a class=\"sub_menu\" title=\"<MTCategoryDescription>\" href=\"<$MTCategoryArchiveLink$>\"><$MTCategoryLabel$></a><br>"; }
?>
</MTCategories>


Working great for me - I can add that to any new blog and instantly get working navigation bars. And of course there's nothing to stop you adding images into the "echoed" code to show say an arrow or bullet gif for the active link.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.