Come visualizzare i tag dei post
Sto convertendo un template HTML in un tema WordPress e sto avendo alcune difficoltà nel visualizzare i tag dei post all'interno del file functions.php. Vorrei aggiungere il seguente codice nel file functions con il codice HTML. Ho provato per alcuni giorni e ho già consultato il codex ma niente di quello che provo funziona.
<div class="tags">
<a href="#" rel="tag">tag 1</a> <a href="#" rel="tag">tag 2</a> <a href="#" rel="tag">tag 3</a> <a href="#" rel="tag">tag 4</a>
</div>
Qualcuno può aiutarmi con questo?
Penso tu stia cercando la funzione get_tags(). Dovrai inserirla nel file single-post.php
(o single.php
se il tuo tema non ha un single-post.php
) (per trovare il template file corretto puoi sempre consultare la Gerarchia dei Template di Wordpress).
Per visualizzare un elenco di tag con la funzione linkata sopra, dovrai usare qualcosa come:
<?php $tags = get_tags(); ?>
<div class="tags">
<?php foreach ( $tags as $tag ) { ?>
<a href="<?php echo get_tag_link( $tag->term_id ); ?> " rel="tag"><?php echo $tag->name; ?></a>
<?php } ?>
</div>

Ecco un modo per aggiungere i tag degli articoli dopo il contenuto solo nei singoli post utilizzando il filtro the_content
in una funzione personalizzata nel tuo file functions.php. Utilizza the_tags
function tags_after_single_post_content($content) {
if( is_singular('post') && is_main_query() ) {
$tags = the_tags('<div class="entry-meta">Contrassegnato con: ',' • ','</div><br />');
$content .= $content . $tags;
}
return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );
Risultato:

Codice corretto:
function tags_after_single_post_content($content) {
$posttags = get_the_tags();
if ($posttags) {
$array = [];
foreach($posttags as $tag) {
$array[] = '<a href="/tag/' . $tag->slug . '/">' . $tag->name . '</a>';
}
$content .= 'Tag: ' . implode(', ', $array) . '<br>';
}
return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );
Il motivo per cui questa risposta è sbagliata è perché the_tags dovrebbe essere utilizzato all'interno di The Loop e the_tags non restituisce nulla, quindi il resto del codice non fa nulla. In questa risposta, get_the_tags restituisce un array di istanze di tag che possiamo quindi aggiungere al contenuto.

Mostra un elenco di tag con link a ciascuno e una classe specifica per ogni tag:
$tags = get_tags();
$html = '<div class="post_tags">';
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= "<a href='{$tag_link}' title='Tag {$tag->name}' class='{$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;

Devi utilizzare questo codice alla fine del file functions.php:
function tags_after_single_post_content($content) {
$posttags = get_the_tags();
if ($posttags) {
$array = [];
foreach($posttags as $tag) {
$array[] = '<a href="/tag/' . $tag->slug . '/">' . $tag->name . '</a>';
}
$content .= 'Tag: ' . implode(', ', $array) . '<br>';
}
return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );

Sembra che tu abbia semplicemente copiato la risposta di Artem. Hai cambiato qualcosa?

ho creato questo codice da arterm con piccole modifiche
function tags_after_single_post_content($content) {
$posttags = get_tags();
if ($posttags) {
$array = [];
foreach($posttags as $tag) {
$array[] = '<a href="/tag/' . $tag->slug . '/">' . $tag->name . '</a>';
}
$content .= '<div class="wulanhastag">Tag: ' . implode(', ', $array) . '</div>';
}
return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );
