ID il corpo in base all'URL - Trucchi CSS

Anonim
 

Questo trasformerebbe http: //domain.tld/blog/home in "blog" (il secondo livello della struttura dell'URL). Se alla radice, tornerà a "casa".

Ecco un metodo alternativo:

Questo trasformerebbe http: //domain.tld/blog/home in "domaintldbloghome", che è molto più specifico. Inoltre rimuoverà le estensioni di file ".php" e il parametro di ricerca predefinito di WordPress.

Metodo più sicuro

function curr_virtdir($echo=true)( $url = explode('/',$_SERVER('REQUEST_URI')); $dir = $url(1) ? $url(1) : 'home'; // defaults to this if in the root $dir = htmlentities(trim(strip_tags($dir))); // prevent injection into the DOM through this function if ($echo) echo $dir; return echo $dir; // ie. curr_virtdir(false) ) function get_curr_virtdir()( curr_virtdir(false); )

Restituisce il valore della directory "centrale":

Su https://css-tricks.com ritornerebbe " home "
Su https://css-tricks.com/snippets restituirebbe " snippets "
Su https://css-tricks.com/forums/viewforum.php ? f = 6 restituirà " forum "

Le funzioni strip_tags () e htmlentities () impediscono che codice dannoso venga inserito nell'URL ed eseguito, ad es.

alert("Booo");

Utilizzo per l'identificazione del corpo:

Altro utilizzo: