Home › Forum › Problemi Vari con WP › Fare in modo che gli articoli di una categoria non vadano nella home.
-
AutorePost
-
-
13 Marzo 2012 alle 20:00 #21079DonovantPartecipante
C’è un modo per evitare che gli articoli di una determinata categoria non vadano a finire nella home page ma restino solo nella categoria?
Grazie.
-
13 Marzo 2012 alle 21:42 #94353TittiModeratore
A me funziona bene questo, nel file functions.php:
function exclude_category($query) {
if ( $query->is_home ) {
$query->set('cat', '-243 -771');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');Dove i numeri sono gli ID delle categorie da escludere
-
14 Marzo 2012 alle 2:16 #94356DonovantPartecipante
Io nel loop avevo fatto cosଠ(conoscendo WP da pochi giorni, anche se l’ho usato una sola volta questa estate)
<?php
while ( have_posts() ) : the_post();
$categoria = get_the_category();
if( single_cat_title( '', false ) == '' && $categoria[0]->cat_name == "APPUNTAMENTI"){
continue;
}
?>
Ma come ho scritto in un altro post, se ho setatto di vedere 4 articoli per pagina, quando, per esempio, 2 di questi quattro fanno parte della categoria che non voglio vedere, nel resto della pagina vedrò solamente i restanti 2 post dei 4 totali per pagina.
La tua funzione la inserisci in function.php, ma poi dove la usi?
add_filter(….) cosa fa?
Grazie mille.
-
14 Marzo 2012 alle 6:54 #94357TittiModeratore
àˆ un filtro applicato alle variabili per modificare la query che genera la lista dei post prima che venga eseguita, cioè prima che acceda al database, con lo scopo di ridurre le chiamate, ma si usa in functions.php, non in un file di pagina, cioè nel punto dove la query principale è eseguita
La funzione si attiva in home, naturalmente (is_home)
Se invece vuoi escludere la categoria dal loop, leggi qua
-
14 Marzo 2012 alle 8:05 #94359DonovantPartecipante
Io cmq non capisco la definizione di function exclude_category($query), $query è un parametro, ma tu questa funzione dove la usi???
non capisco proprio.
Crei una funzione che usi dove? e add_filter(‘pre_get_posts’, ‘exclude_category’); come da te scritto, serve a modificare la query.
-
14 Marzo 2012 alle 8:09 #94360wollyAmministratore del forum
-
14 Marzo 2012 alle 8:13 #94361DonovantPartecipante
Eh si la stavo leggendo…ma ancora non ho ben capito come usarla, e come cmq viene definita quella funzione, cioè la implementa, ma dove la richiama?
-
14 Marzo 2012 alle 8:45 #94363
-
14 Marzo 2012 alle 8:58 #94364DonovantPartecipante
scusatemi assaieee, ma non ho ancora dormito…non avevo visto che il nome della funzione veniva passato come stringa alla funzione add_filter(…) , grazie mille!!!
-
14 Marzo 2012 alle 9:08 #94365DonovantPartecipante
Anche se vorrei ancora che qualcuno mi spiegasse, come viene trattata la variabile $query, è un parametro, viene passato alla funzione implementata, ma all’interno della stessa viene trattata ($query) come se fosse un oggetto già instanziato.
Grazie.
-
14 Marzo 2012 alle 9:18 #94366DonovantPartecipante
Ok letto: Special note: The $query object passed to your function is passed by reference. You do not need to declare globals or return a value. Any changes you make to the object from inside your function are made to the original immediately.
-
-
AutorePost
- Devi essere connesso per rispondere a questo topic.