WordPress.org

Forums

Need help with checking current page (2 posts)

  1. littleL
    Member
    Posted 1 year ago #

    Hi

    I need some help please, I'm not very familiar with php.

    How would you check something like the following code dynamically?

    if ( is_page('home') ) { do something }
    elseif ( is_page('about') ) { do something }
    ...

    I'm trying to add css class to the actual links for the nav menu, I found this code which works lovely

    function add_nav_class($output) {
    $output= preg_replace('/<a/', '<a class="your-class"', $output, -1);
    return $output;
    }
    add_filter('wp_nav_menu', 'add_nav_class');

    but I need to do something like this, which has a different class if you are on that page.

    function add_nav_class($output) {
    if (is_page(WHAT_GOES_HERE?)) { $menuclass = '<a class="menuOn"'; }
    else { $menuclass = '<a class="menuOff"'; }
    
    $output= preg_replace('/<a/', $menuclass, $output, -1);
    return $output;
    }
    add_filter('wp_nav_menu', 'add_nav_class');

    hope that makes sense..
    many thanks!

  2. vtxyzzy
    Member
    Posted 1 year ago #

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags