Skip to content

Instantly share code, notes, and snippets.

@inc2734
Last active August 29, 2015 14:13
Show Gist options
  • Save inc2734/03d23ce33eec770ec784 to your computer and use it in GitHub Desktop.
Save inc2734/03d23ce33eec770ec784 to your computer and use it in GitHub Desktop.
WordPressで一番親のディレクトリ名を取得する。サブディレクトリインストール型対応。
<?php
/**
* get_second_directory
*/
function get_second_directory() {
if ( $_SERVER['REQUEST_URI'] ) {
$home_url = home_url();
$home_url = explode( '/', $home_url );
$REQUEST_URI = $_SERVER['REQUEST_URI'];
$REQUEST_URI = preg_replace( '/^(.*)?\?.*$/', '$1', $REQUEST_URI );
$REQUEST_URI = trim( $REQUEST_URI, '/' );
$directories = explode( '/', $REQUEST_URI );
$over = 0;
if ( count( $home_url ) >= 4 ) {
$over = count( $home_url ) - 3;
}
foreach ( $directories as $directory ) {
if ( $directory ) {
if ( $over ) {
$over --;
continue;
}
return $directory;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment