WordPress Plugin: Hot’n'Cold
Hot’n'Cold è un plugin per WordPress che mostra targhette colorate diverse a seconda della popolarità dei post, basandosi sulle statistiche di Jetpack.

Avete creato una custom taxonomy in WP, perché magari volevate qualcosa di più dei normali “Articoli, che magari seguissero regole diverse e forse anche per essere visualizzati diversamente.
E se voleste visualizzare nel template questi post, nella stessa pagina, MA prima quelli con un valore specifico in un Campo Personalizzato, e poi gli altri?
Come fate a impedire che il Loop carichi tutti i post, senza tener conto di questo valore nei campi personalizzati?
Si fa così, usando due custom Loop.
Ovviamente dovete sostituire con i vostri valori il nome del custom type post, nome e valore del campo personalizzato in questione, dove necessario.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php // Loop 1 ?> <?php // questa visualizza prima tutti post con custom taxonomy post type "PostTypeName", che HANNO che hanno come value "CustomFieldValue" nel campo personalizzato query_posts(array('post_type' => 'PostTypeName', 'meta_value' => 'CustomFieldValue')); if(have_posts()) : while(have_posts()) : the_post(); ?> <h3><?php the_title(); ?></h3> <p><?php the_excerpt(); ?></p> <div>some html</div> <?php // se volete visualizzare proprio il valore del "CustomFieldName" del post, usare questo echo get_post_meta($post->ID, 'CustomFieldName', true); ?> <h3><?php the_title(); ?></h3> <p><?php the_excerpt(); ?></p> <div>html a piacere</div> <?php endwhile; endif; wp_reset_query(); ?> |
Siete a metà strada adesso, vediamo ora come visualizzare i post mancanti, quelli che quel valore nel campo personalizzato NON ce l’hanno:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php // 2nd loop ?> <?php // questa visualizza tutti gli altri post con custom taxonomy post type "PostTypeName", che NON che hanno come value "CustomFieldValue" nel campo personalizzato $querystr = " SELECT $wpdb->posts.* FROM $wpdb->posts, $wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_value = 'CustomFieldValue' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'PostTypeName' ORDER BY $wpdb->posts.post_date DESC "; $pageposts = $wpdb->get_results($querystr, OBJECT); if ($pageposts): foreach ($pageposts as $post): setup_postdata($post); ?> <h3><?php the_title(); ?></h3> <?php the_excerpt(); ?> <div>html a piacere</div> <?php endforeach; endif; ?> |
Questa seconda Loop in realtà può essere costruita anche così:
|
1 2 3 4 5 6 7 |
<?php // questa visualizza tutti gli altri post con custom taxonomy post type "PostTypeName", che NON che hanno come value "CustomFieldValue" nel campo personalizzato query_posts(array('post_type' => 'PostTypeName', 'meta_value' => 'CustomFieldValue', 'meta_compare' => '!=', 'meta_key' => 'CustomFieldName')); if(have_posts()) : while(have_posts()) : the_post(); ?> <h3><!--?php the_title(); ?></h3> <?php the_excerpt(); ?> <div>some html</div> <?php endwhile; endif; wp_reset_query(); ?> |
Enjoy!