WordPress.org

Ready to get started?Download WordPress

Forums

Get Parent Slug (4 posts)

  1. Danny159
    Member
    Posted 2 years ago #

    Hey,

    I am making a theme, and I want to include different menu's depending on the parent slug...

    For example if the user is on a page like

    /cat1/my-page/ -> It will show menu 1
    /cat2/my-page/ -> Will show the menu for cat2?

    I can find any function to allow this?
    Does anyone know how I can fix this problem.

    Dan

  2. esmi
    Forum Moderator
    Posted 2 years ago #

  3. Danny159
    Member
    Posted 2 years ago #

    That doesnt seem to show me the slug?

    What I want to do is in my template I want to do an if statement... eg...

    if($parent_slug == "cat1"){
        wp_nav_menu(array('theme_location' => 'main', 'menu_class' => 'simple-nav', 'container' => false));
    }else{
        wp_nav_menu(array('theme_location' => 'cat2', 'menu_class' => 'simple-nav', 'container' => false));
    }

    [ Please do not bump, it's not permitted here. ]

  4. Danny159
    Member
    Posted 2 years ago #

    Ok I sorted this, I didnt think you could run any php in a template file but you can... so here is how I fixed it just incase anyone wants to know:

    <?php
    $parentslug = explode("/", $_SERVER['REQUEST_URI']);
    if($parentslug[1] == "parent1"){
    	$theNav = "nav5";
    }else
    if($parentslug[1] == "parent2"){
    	$theNav = "nav4";
    }else
    if($parentslug[1] == "parent3"){
    	$theNav = "nav3";
    }else
    if($parentslug[1] == "parent4"){
    	$theNav = "nav2";
    }else{
    	$theNav = "aboutnav";
    }
    
    wp_nav_menu(array('theme_location' => $theNav, 'items_wrap' => '<ul>%3$s</ul>', 'container' => false));
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.