Vuoi visualizzare un elenco di categorie ordinate in base alla categoria aggiornata più di recente? Sebbene esista probabilmente un plugin per questo, abbiamo creato un rapido snippet di codice che puoi utilizzare per ordinare le categorie in base all'aggiornamento più recente in WordPress.
Istruzioni:
Tutto quello che devi fare è aggiungere questo codice al file del template del tuo tema o in un plugin specifico per il sito:
<?php
$cat_array = array();
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 20,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$cat_args=array('orderby' => 'none');
$cats = wp_get_post_terms( $post->ID , 'category', $cat_args);
foreach($cats as $cat) {
$cat_array[$cat->term_id] = $cat->term_id;
}
endwhile;
}
if ($cat_array) {
foreach($cat_array as $cat) {
$category = get_term_by('ID',$cat, 'category');
echo '<a href="' . esc_attr(get_term_link($category, 'category')) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>'.'<br />';
}
}
wp_reset_query();
?>
Nota: Se questa è la prima volta che aggiungi snippet di codice in WordPress, consulta la nostra guida su come copiare / incollare correttamente snippet di codice in WordPress, in modo da non rompere accidentalmente il tuo sito.
Se ti è piaciuto questo snippet di codice, prendi in considerazione la lettura dei nostri altri articoli sul sito come: Come creare un modulo di contatto in WordPress e 12 migliori plugin per podcast di WordPress.
Questa funzione non funziona!!