Thematic: Come cambiare Postheader

Cambiare il postheader in Thematic Framework per WordPress e’ possibile ma come fare senza mettere mano a index.php? Certo, modificarlo è possibile ma non è questo lo scopo di un child theme. Ad ogni nuovo aggiornamento di Thematic o di WordPress dovremmo di nuovo cambiare il nostro file.

Questa è la situazione di partenza:

Il postheader predefinito (clicca per ingrandire)

Il postheader predefinito (clicca per ingrandire)

e qui il risultato che vogliamo ottenere:

Il postheader da noi modificato (clicca per ingrandire)

Il postheader da noi modificato (clicca per ingrandire)

Vediamo come fae.

Diamo uno sguardo a hooks-filters.php nella cartella library/extensions del tema Thematic. Troviamo la seguente funzione:

// Informazioni in Post Header
function thematic_postheader() {
global $post, $authordata;
if (is_single() || is_page()){
$posttitle = '<h1 class="entry-title">' . get_the_title() . "</h1>n";
} elseif (is_404()){
$posttitle = '<h1 class="entry-title">' . __('Not Found', 'thematic') . "</h1>n";
} else {
$posttitle = '<h2 class="entry-title"><a href="';
$posttitle .= get_permalink();
$posttitle .= '" title="';
$posttitle .= __('Permalink to ', 'thematic') . the_title_attribute('echo=0');
$posttitle .= '" rel="bookmark">';
$posttitle .= get_the_title();
$posttitle .= "</a></h2>n";
}
$postmeta = '<div class="entry-meta">';
$postmeta .= '<span class="author vcard">';
$postmeta .= __('By ', 'thematic') . '<a class="url fn n" href="';
$postmeta .= get_author_link(false, $authordata->ID, $authordata->user_nicename);
$postmeta .= '" title="' . __('View all posts by ', 'thematic') . get_the_author() . '">';
$postmeta .= get_the_author();
$postmeta .= '</a></span><span class="meta-sep"> | </span>';
$postmeta .= '<span class="entry-date"><abbr class="published" title="';
$postmeta .= get_the_time('Y-m-dTH:i:sO') . '">';
$postmeta .= get_the_time('F j, Y');
$postmeta .= '</abbr></span>';
$postmeta .= "</div><!– .entry-meta –>n";
if ($post->post_type == 'page' || is_404()){
$postheader = $posttitle;
} else {
$postheader = $posttitle . $postmeta;
}
echo apply_filters( 'thematic_postheader', $postheader ); // Filtro per sovrascrivere il post header preimpostato
}

Questa funzione viene usata da Thematic per generare il Postheader e richiamarlo in header.php. L’output completo del postheader e’ in $postheader. In un “normale” tema di WordPress troveremmo il codice direttamente in header.php mentre in Thematic Framework per WordPress e’ una funzione  che possiamo modificare a nostro piacimento. In $postheader troviamo il titolo dell’ articolo, la data e l’autore. Mettiamo che vogliamo nascondere alcune di queste informazioni ed aggiungerne delle altre. Preferiamo mostrare la data a sinistra, la categoria ed il link “Lascia un commento” sotto il titolo: come ci arriviamo?

Per prima cosa, copiamo la funzione vista sopra in functions.php presente nella cartella del nostro Child Theme e rinominiamola (per esempio altamentedecorativo_postheader() ).

function altamentedecorativo_postheader() {

Poi inseriamo fuori dalla funzione il seguente codice:

add_filter ('thematic_postheader', 'altamentedecorativo_postheader'); // IMPORTANTISSIMO!

E’ importantissimo perche’ e’ la chiave per sovrascrivere la funzione nativa con la nostra.
Ecco la funzione completa, l’ analizzeremo poi nei dettagli.

function altamentedecorativo_postheader() {
global $post, $authordata,$id;
if (is_single() || is_page()) {
$posttitle = '<h1 class="entry-title">' . get_the_title() . "</h1>n";
} elseif (is_404()) {
$posttitle = '<h1 class="entry-title">' . __('Not Found', 'thematic') . "</h1>n";
} else {
// Muoviamo la data del post da Postfooter a Postheader
$posttitle = '<div class="post-date">';
$posttitle .= '<span class="post-month">';
$posttitle .= get_the_time('M');
$posttitle .= '</span>';
$posttitle .= '<span class="post-day">';
$posttitle .= get_the_time('d');
$posttitle .= '</span>';
$posttitle .= '</div>';
// Fine
$posttitle .= '<div class="post-title">';
$posttitle .= '<h2 class="entry-title"><a href="';
$posttitle .= get_permalink();
$posttitle .= '" title="';
$posttitle .= __('Permalink to ', 'thematic') . the_title_attribute('echo=0');
$posttitle .= '" rel="bookmark">';
$posttitle .= get_the_title();
$posttitle .= "</a></h2>n";
$posttitle .= '</div>';
$postmeta = '<div class="entry-meta">';
// Mostra il link ai commenti ed il link edit
if (comments_open()) {
$postcommentnumber = get_comments_number();
if ($postcommentnumber > '1') {
$postmeta .= ' <span class="comment"><a href="' . get_permalink() . '#comments" title="' . __('Comment on ', 'thematic') . the_title_attribute('echo=0') . '">';
$postmeta .= get_comments_number() . __(' Comments', 'thematic') . '</a></span>';
} elseif ($postcommentnumber == '1') {
$postmeta .= ' <span class="comment"><a href="' . get_permalink() . '#comments" title="' . __('Comment on ', 'thematic') . the_title_attribute('echo=0') . '">';
$postmeta .= get_comments_number() . __(' Comment', 'thematic') . '</a></span>';
} elseif ($postcommentnumber == '0') {
$postmeta .= ' <span class="comment"><a href="' . get_permalink() . '#comments" title="' . __('Comment on ', 'thematic') . the_title_attribute('echo=0') . '">';
$postmeta .= __('Leave a comment', 'thematic') . '</a></span>';
}
} else {
$postmeta = ' <span class="comment">' . __('Comments closed', 'thematic') .'</span>';
}
//$postmeta .= '<span class="author vcard">';
//$postmeta .= __('By ', 'thematic') . '<a class="url fn n" href="';
//$postmeta .= get_author_link(false, $authordata->ID, $authordata->user_nicename);
//$postmeta .= '" title="' . __('View all posts by ', 'thematic') . get_the_author() . '">';
//$postmeta .= get_the_author();
//$postmeta .= '</a></span><span class="meta-sep"> | </span>';
//$postmeta .= '<span class="entry-date"><abbr class="published" title="';
//$postmeta .= get_the_time('Y-m-dTH:i:sO') . '">';
//$postmeta .= get_the_time('j. F, Y');
//$postmeta .= '</abbr></span>';
//$postmeta .= '<div class="entry-utility">';
// Mostra Categorie
$postmeta .= '<span class="category-links">';
if (is_single()) {
$postmeta .= __('', 'thematic') . get_the_category_list(', ');
$postmeta .= '</span>';
} elseif ( is_category() && $cats_meow = thematic_cats_meow(', ') ) { /* Returns categories other than the one queried */
$postmeta .= __(' ', 'thematic') . $cats_meow;
$postmeta .= '</span> <span class="meta-sep">|</span>';
} else {
$postmeta .= __(' ', 'thematic') . get_the_category_list(', ');
$postmeta .= '</span> <span class="meta-sep">|</span>';
}
//$postmeta .= "</div><!– .entry-utility –>n";
$postmeta .= "</div><!– .entry-meta –>n";
}
if ($post->post_type == 'page' || is_404()) {
$postheader = $posttitle;
} else {
$postheader = $posttitle . $postmeta;
}
echo apply_filters( 'altamentedecorativo_postheader', $postheader ); // Filtro per sovrascrivere il postheader predefinito
}
add_filter ('thematic_postheader', 'altamentedecorativo_postheader'); // IMPORTANTISSIMO!

Al commento

// Muoviamo la data del post da Postfooter a Postheader

possiamo vedere le modifiche cha abbiamo fatto per mostrare la data dove volevamo. Abbiamo solamente aggiunto queste linee di codice alla funzione $postheader per avere delle nuove Classi CSS e la data dell’articolo mostrata. Non dimentichiamoci poi di aggiungere in styles.css le informazioni per lo stile delle nuove Classi create.

Gli altri cambiamenti li troviamo in

// Mostra il link ai commenti ed il link edit

per mostrare il link “Leave a Comment” nel postheader e in

// Mostra Categorie

per mostrare le categorie nell’ header. Lo abbiamo semplicemente copiato dalla funzione thematic_postfooter() in hooks-filters.php.

Per cambiare il postfooter guardiamo sempre in hooks-filters.php e cerchiamo la funzione thematic_postfooter(): possiamo modificarlo come vogliamo nello stesso modo in cui abbiamo modificato postheader, altrimenti avremo un output doppio di alcune delle informazioni che abbiamo aggiunto a postheader.

Per domande o dimostrazioni su come avete apportato queste modifiche, lasciate un commento!
[wordcloud]Wordle: Thematic: Come cambiare Postheader


Articolo originariamente pubblicato su Bendler.tv. Thanks Bjorn!

Tradotto e adattato con l’esplicito consenso dell’ autore.

Termini collegati: , , I commenti ed i trackbacks sono attualmente chiusi.