if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Restituisce il valore da usare come slug/classe per un contenuto.
*
* La home del tema è una pagina Gutenberg normale impostata come pagina
* frontale: il suo slug editoriale può cambiare, ma la classe deve restare
* stabile e semantica.
*
* @param WP_Post $post Post da identificare.
* @return string
*/
function mm_slug_body_class_slug( $post ) {
if ( ! $post instanceof WP_Post ) {
return '';
}
if ( (int) get_option( 'page_on_front' ) === (int) $post->ID ) {
return 'home';
}
return sanitize_title( $post->post_name );
}
/** Aggiunge il meta box a pagine, post e CPT pubblici. */
add_action(
'add_meta_boxes',
function () {
foreach ( get_post_types( array( 'public' => true ), 'names' ) as $post_type ) {
add_meta_box(
'mm_slug_body',
'Slug (classe del body)',
'mm_slug_body_meta_box',
$post_type,
'side',
'default'
);
}
}
);
/**
* @param WP_Post $post Post corrente nell'editor.
*/
function mm_slug_body_meta_box( $post ) {
$slug = mm_slug_body_class_slug( $post );
if ( '' === $slug ) {
echo '
Lo slug verrà generato al salvataggio.
';
return;
}
echo 'Classe del body: ' . esc_html( sanitize_html_class( $slug ) ) . '
';
}
/** Aggiunge lo slug come classe del body nelle pagine singole e nella home statica. */
add_filter(
'body_class',
function ( $classes ) {
$post = null;
// Caso esplicito per la pagina frontale del template Gutenberg.
if ( is_front_page() ) {
$front_page_id = (int) get_option( 'page_on_front' );
if ( $front_page_id > 0 ) {
$post = get_post( $front_page_id );
}
} elseif ( is_singular() ) {
$queried_object = get_queried_object();
if ( $queried_object instanceof WP_Post ) {
$post = $queried_object;
}
}
$slug = mm_slug_body_class_slug( $post );
if ( '' !== $slug ) {
$class = sanitize_html_class( $slug );
if ( ! in_array( $class, $classes, true ) ) {
$classes[] = $class;
}
}
return $classes;
}
);
Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/martamalengo.com/wp-content/themes/martamalengo/inc/body-class.php:1) in /var/www/html/martamalengo.com/wp-includes/pluggable.php on line 1539
Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/martamalengo.com/wp-content/themes/martamalengo/inc/body-class.php:1) in /var/www/html/martamalengo.com/wp-includes/pluggable.php on line 1542