2015-06-28 14 views
6

Sto tentando di visualizzare la tassonomia personalizzata nella pagina autore con un contatore ma sembra che non sappia come farlo.Wordpress: come visualizzare il numero di post nella pagina autore per tassonomia personalizzata

Ho un codice in function.php

add_action('pre_get_posts', function ($q) { 

    if(!is_admin() && $q->is_main_query() && $q->is_author()) { 

     $q->set('posts_per_page', 100); 
     $q->set('post_type', 'custom_feedback'); 

    } 

}); 

e nel mio autore:

<div class="feedback-respond"> 
     <h3 class="feedback-title">User Feedback </h3> 
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
       <?php the_content(); ?> 
      <?php endwhile; else: ?> 
       <p><?php _e('No posts by this author.'); ?></p> 
      <?php endif; ?> 
      </div> 

Il codice funziona per tutto il profilo dell'autore, ma non so come ottenere la tassonomia personalizzata per visualizzare in questo modo:

Feedback utente

6 POSITIVO valutazioni 4 risposte NEGATIVE

tutti i feedback va qui

tutti i feedback va qui

tutti i feedback va qui

Tra l'altro si tratta di un tipo di messaggio personalizzato (custom_feedback) e tassonomia personalizzata (feedback_taxonomy) con due categorie Positivo e Negativo.

Si prega di aiutare i maestri?

+0

https://codex.wordpress.org/ Function_Reference/get_post_custom – odedta

+0

ma non si tratta di campi personalizzati, è la tassonomia personalizzata in author.php visualizzata. – rolex

+0

Consentitemi di Google che per voi: http://wordpress.stackexchange.com/questions/10175/how-to-display-custom-taxonomies-in-posts – odedta

risposta

1

L'unico modo per ottenere ciò sarà eseguire due query separate e contare i post restituiti dalle due query separate. Per questo useremo get_posts come get_posts già passa alcune impostazioni predefinite importanti a WP_Query per rendere la query più veloce e più orientata alle prestazioni.

Aggiungeremo un tempo enorme e un risparmio di risorse alla query, 'fields' => 'ids'. Ciò che fa è che recupera solo i post ID e non l'oggetto post completo. Questo può ridurre il tempo di interrogazione e le query db del 99%, quindi anche se si eseguiranno 2 query separate sul database completo, la perdita di prestazioni della pagina sarà impercettibile.

Consente di mettere tutto in codice (Questo va in author.php, e nota, questo codice non è testato e ha bisogno di almeno PHP 5.4+)

$author_id = get_queried_object_id(); // Gets the author id when viewing the author page 

// Add our term slugs into an array. 
$terms = ['positive', 'negative']; // Just make sure these values are correct 
$count = []; 
foreach ($terms as $term) { 
    // Build our query 
    $args = [ 
     'nopaging' => true, 
     'post_type' => 'custom_feedback', 
     'author' => $author_id, 
     'tax_query' => [ 
      [ 
       'taxonomy' => 'feedback_taxonomy', 
       'field' => 'slug', 
       'terms' => $term 
      ], 
     ], 
     'fields' => 'ids' 
    ]; 
    $q = get_posts($args); 

    // Count the amount of posts and add in array 
    $count[$term] = count($q); 
} 

// Display our text with post counts, just make sure your array keys correspond with your term slugs used 
$positive = (isset($count['positive'])) ? $count['positive'] : 0; 
$negative =(isset($count['negative'])) ? $count['negative'] : 0; 

echo $positive . ' POSITIVE feedback ' . $negative . ' NEGATIVE feedback'; 
+0

funziona perfettamente. grazie mille @PieterGoosen. Posso chiedere ancora una cosa?c'è un modo per modificare the_content()? Vorrei aggiungere una classe CSS alla tassonomia negativa e positiva. – rolex

+0

Piacere mio, felice che funzioni. Sì, ciò può essere fatto. Dovresti iniziare una nuova domanda per questo però :-) –

+0

ok lo farò..grazie molto. – rolex

Problemi correlati