Stai cercando un modo per disabilitare completamente la funzionalità di ricerca sul tuo sito web? Sebbene probabilmente esista un plugin per questo, abbiamo creato un rapido snippet di codice che puoi utilizzare per disabilitare completamente la ricerca pubblica in WordPress.
Istruzioni:
Tutto quello che devi fare è aggiungere questo codice al file functions.php del tuo tema o in un plugin specifico per il sito:
<?php tag_cloud_by_category($cat_id); // $cat_id is the desired category id ?>
Puoi anche raccogliere tutti i tag utilizzati da una categoria per qualsiasi altra cosa di cui hai bisogno, in questo modo:
<?php $tags = get_tags_in_use($cat_id, 'format'); // Format can be 'id', 'name', or 'slug' ?>
Per visualizzare la nuvola di tag, aggiungi semplicemente questo codice al file functions.php del tuo tema.
// Get tags IN USE by category id
function get_tags_in_use($category_ID, $type = 'name'){
// Set up the query for our posts
$my_posts = new WP_Query(array(
'cat' => $category_ID, // Your category id
'posts_per_page' => -1 // All posts from that category
));
// Initialize our tag arrays
$tags_by_id = array();
$tags_by_name = array();
$tags_by_slug = array();
// If there are posts in this category, loop through them
if ($my_posts->have_posts()): while ($my_posts->have_posts()): $my_posts->the_post();
// Get all tags of current post
$post_tags = wp_get_post_tags($my_posts->post->ID);
// Loop through each tag
foreach ($post_tags as $tag):
// Set up our tags by id, name, and/or slug
$tag_id = $tag->term_id;
$tag_name = $tag->name;
$tag_slug = $tag->slug;
// Push each tag into our main array if not already in it
if (!in_array($tag_id, $tags_by_id))
array_push($tags_by_id, $tag_id);
if (!in_array($tag_name, $tags_by_name))
array_push($tags_by_name, $tag_name);
if (!in_array($tag_slug, $tags_by_slug))
array_push($tags_by_slug, $tag_slug);
endforeach;
endwhile; endif;
// Return value specified
if ($type == 'id')
return $tags_by_id;
if ($type == 'name')
return $tags_by_name;
if ($type == 'slug')
return $tags_by_slug;
}
// Output tag cloud based on category id
function tag_cloud_by_category($category_ID){
// Get our tag array
$tags = get_tags_in_use($category_ID, 'id');
// Start our output variable
echo '<div class="tag-cloud">';
// Cycle through each tag and set it up
foreach ($tags as $tag):
// Get our count
$term = get_term_by('id', $tag, 'post_tag');
$count = $term->count;
// Get tag name
$tag_info = get_tag($tag);
$tag_name = $tag_info->name;
// Get tag link
$tag_link = get_tag_link($tag);
// Set up our font size based on count
$size = 8 + $count;
echo '<span style="font-size:'.$size.'px;">';
echo '<a href="'.$tag_link.'">'.$tag_name.'</a>';
echo ' </span>';
endforeach;
echo '</div>';
}
Nota: Se questa è la prima volta che aggiungi snippet di codice in WordPress, consulta la nostra guida su come aggiungere correttamente snippet di codice in WordPress, in modo da non compromettere accidentalmente il tuo sito.
Se ti è piaciuto questo snippet di codice, prendi in considerazione la possibilità di consultare i nostri altri articoli sul sito come: i 9 migliori plugin per eventi di WordPress e come creare splendidi moduli di opt-in per WordPress.
Grazie, mi hai migliorato la giornata, non riesco a credere che nessuno dei plugin disponibili avesse questa opzione per filtrare i tag per categoria.
L'ho creato come shortcode e l'ho inserito in una pagina Elementor, solo che ho dovuto apportare una correzione in modo che non interrompesse Elementor.
Alla riga 39, dopo endwhile; e prima di endif;, ho dovuto aggiungere la seguente riga per ripristinare i dati originali del post:
wp_reset_postdata();
È QUASI quello che stavo cercando.
Ho un tipo di post personalizzato 'download' con il suo tag personalizzato 'download_tag' (utilizzando Easy Digital Downloads)
Come posso adattare il tuo codice per mostrare il 'download_tag' di una 'download_category' fornita?
Dovresti modificare la chiamata a WP_Query() per includere il tipo di post personalizzato.