Last active
          September 11, 2022 10:18 
        
      - 
      
- 
        Save reformatco/2050574 to your computer and use it in GitHub Desktop. 
    Top-Level Slug Function for Wordpress
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function top_slug($post){ | |
| if(count($post->ancestors)>0){ | |
| /* if in a sub section */ | |
| $parent_permalink = get_permalink($post->ancestors[count($post->ancestors)-1]); | |
| echo basename($parent_permalink); | |
| }else{ | |
| $permalink = get_permalink($post->ID); | |
| if($permalink===get_bloginfo('url')."/"){ | |
| /* if on home page */ | |
| return "home"; | |
| }else{ | |
| /* if in a top level section */ | |
| return basename($permalink); | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment