Come migliorare questa funzione grezza in WordPress?
25 gen 2011, 06:45:58
Visualizzazioni: 148
Voti: 0
Nella prima funzione qui sotto, all'interno del foreach, sto richiamando la seconda funzione per verificare la presenza di ID di categoria che voglio rimuovere dall'elenco delle categorie.
Tuttavia, il modo in cui lo sto facendo nella seconda funzione mi sembra piuttosto grezzo, per usare un eufemismo. Come potrei migliorare questa ricerca?
function admin_trim_category_description( $terms, $taxonomies )
{
if( 'category' != $taxonomies[0] )return $terms;
foreach( $terms as $key=>$term )
{
$terms[$key]->description = strip_tags(substr( $term->description, 0, 75 ))."...";
if(ce4_get_utility_cats($terms[$key]->term_id))
{
unset($terms[$key]);
}
}
return $terms;
}
function ce4_get_utility_cats($cat_id)
{
if($cat_id == get_cat_ID('category1') OR $cat_id == get_cat_ID('hidden') OR $cat_id == get_cat_ID('category2') OR $cat_id == get_cat_ID('category3'))
{
return true;
}
else
{
return false;
}
}
MODIFICA: Le funzioni sopra vengono chiamate nel seguente modo...
add_action( 'admin_head-edit-tags.php', 'admin_edit_tags' );
function admin_edit_tags(){
add_filter( 'get_terms', 'admin_trim_category_description', 10, 2 );
}
Commenti
Tutte le risposte alla domanda
3
1
function admin_trim_category_description( $terms, $taxonomies ){
if( 'category' != $taxonomies[0] )return $terms;
$whatever_categories = array('category1', 'hidden', 'category2', 'category3');
foreach( $terms as $key => $term)
if(!in_array($terms[$key]->name, $whatever_categories)) $terms[$key]->description = strip_tags(substr( $term->description, 0, 75 ))."...";
else unset($terms[$key]);
return $terms;
}

onetrickpony
13.6K
25 gen 2011 07:19:02
0
L'unica cosa che posso vedere che è palesemente inefficiente è il fatto che chiami get_cat_ID
per ogni categoria per ogni termine. Ogni chiamata a questa funzione comporta una query al database.
Sarebbe più efficiente chiamare queste funzioni prima del ciclo foreach
e inserirle in un array da passare come parametro alla funzione.

Cronco
523
25 gen 2011 07:20:22
Domande correlate
4
risposte
1
risposte