Per convertire i tag WordPress in hashtag, puoi utilizzare una funzione personalizzata nel tuo tema WordPress. Ecco un esempio PHP di come potrebbe essere implementato:
function convert_tags_to_hashtags() {
// Ottieni i tag correnti dell'articolo
$tags = get_the_tags();
// Verifica se ci sono dei tag
if ($tags) {
// Inizializza una stringa vuota per i nuovi tag con gli hashtag
$new_tags = '';
// Itera sui tag e aggiungi gli hashtag
foreach ($tags as $tag) {
$new_tags .= '#' . $tag->name . ' ';
}
// Restituisci i nuovi tag con gli hashtag
return $new_tags;
}
// Se non ci sono tag, restituisci una stringa vuota
return '';
}
Puoi quindi utilizzare la funzione convert_tags_to_hashtags() nel tuo file del tema WordPress per visualizzare i tag con gli hashtag nel modo desiderato. Ad esempio, puoi inserire questa funzione all’interno del file single.php o content.php per visualizzare i tag come hashtag all’interno di un articolo.
Alternativa:
function wptag_to_hashtag($mywp_post)
{
$content = $mywp_post->post_content;
$ID = $mywp_post->ID;
preg_match_all('/B(#[a-zA-Z]+b)/', $content, $matches, PREG_PATTERN_ORDER);
if (isset($matches[1])) {
foreach ($matches[1] as $matchKey) {
wp_set_post_tags($ID, $matchKey, true);
}
}
}
add_action('post_save', 'wptag_to_hashtag', 10, 2);
function hashtagger($content)
{
$siteurl = get_site_url(); // get wordpress siteurl to create url for tags
$content = preg_replace('/([^a-zA-Z-_&])#([a-zA-Z_]+)/', "$1<a class="hashtags" href="$siteurl/tag/$2" target="_parent" >#$2</a>", $content);
return $content;
}
add_filter('the_content', 'hashtagger');
Aggiungi questo snippet al file functions.php del tuo tema attivo.

Commenta per primo