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 );
    }
3
Commenti

Da dove stai chiamando admin_trim_category_description()? Qual è il tuo contesto e caso d'uso? Inoltre, cosa contiene $terms?

MikeSchinkel MikeSchinkel
25 gen 2011 07:20:47

E questa è una buona lista di funzioni per array in php: http://www.w3schools.com/php/php_ref_array.asp

hakre hakre
25 gen 2011 07:23:58

@Mike: Ottimo punto, ho aggiornato la mia domanda per fornire il contesto

Scott B Scott B
25 gen 2011 17:38:12
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;
}
25 gen 2011 07:19:02
Commenti

+1 Funziona perfettamente. Sapevo che ci fosse un modo migliore :) in_array tornerà utile in futuro.

Scott B Scott B
25 gen 2011 17:44:52
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.

25 gen 2011 07:20:22
0

Forse prova ad aggiungere i termini che vuoi verificare in un array, e poi controlla semplicemente se $cat_id è nell'array. Vorrei dire che esistono funzioni simili a questa, ma al momento non mi vengono in mente. Eliminerebbe la necessità degli OR.

25 gen 2011 07:22:13