Skip to content

Instantly share code, notes, and snippets.

@reformatco
Last active September 11, 2022 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reformatco/2050574 to your computer and use it in GitHub Desktop.
Save reformatco/2050574 to your computer and use it in GitHub Desktop.
Top-Level Slug Function for Wordpress
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