WordPress assegna delle classi all'elemento body
in base alla sezione nella quale ci troviamo. Purtroppo non assegna un ID univoco a tale elemento. Perchè è importante? Perchè ai fini degli stili CSS da applicare un ID è più specifico di una classe. Vediamo come ovviare a questo problema.
Possiamo usare la seguente funzione (migliorabile) da aggiungere al file functions.php
:
if(!function_exists('body_id')) {
function body_id() {
global $post;
$post_id = $post->ID;
$id = '';
if(is_home() || is_front_page()) {
$id = 'home';
}
if(is_single()) {
$id = 'post-' . $post_id;
}
if(is_page()) {
$id = 'page-' . $post_id;
}
if(is_category() || is_archive()) {
$cat_name = get_query_var('category_name');
if(!empty($cat_name)) {
$id = 'category-' . $cat_name;
} else {
// Qui potete aggiungere dei test per le date
$id = 'archive';
}
}
if(is_tag()) {
$id = 'tag-' . get_query_var('tag');
}
if(is_404()) {
$id = 'error404';
}
echo ' id="'. $id .'" ';
}
}
Possiamo usare tale funzione nel file header.php
:
<body <?php body_id();?>>