metinsaylan // rogue developer

A simple trick for testing current page using page slugs

//

While designing custom navigations, custom page templates, custom sidebar elements you may need to test current page. Here is a simple trick for getting current page. *** The trick is using page slug inside is_page function. For example on your “About” page you can use:

<?php if(is_page('about')){ ?>

Put some HTML code here for About page.

<?php } ?>

This code tests if the current page is about page, and it will show the code in between if is_page returns true.

You can also create a simple function to echo “current” class for the current page using this function:

<?php

function stf_is_current($slug){ if(is_page($slug)){ return " current"; } }

  // Sample link
  echo '<li><a href="http://metinsaylan.com/contact" class="'.stf_is_current('contact').'">Contact</a></li>';

?>

This code will output the link to contact page with current class on Contact page only. You can use this simple trick on your custom navigation templates. It is generally used for styling current page link.

I hope you found it useful.

Search A simple trick for testing current page using page slugs on Google