Cum să afișezi etichetele (tag-urile) articolelor
Convertesc un șablon HTML într-o temă WordPress și întâmpin dificultăți în afișarea etichetelor articolelor în fișierul functions.php. Aș dori să adaug următorul cod HTML în fișierul functions. Am încercat de câteva zile și am consultat deja codexul, dar nimic din ce am încercat nu funcționează.
<div class="tags">
<a href="#" rel="tag">etichetă 1</a> <a href="#" rel="tag">etichetă 2</a> <a href="#" rel="tag">etichetă 3</a> <a href="#" rel="tag">etichetă 4</a>
</div>
Poate cineva să mă ajute cu acest lucru?
Cred că cauți funcția get_tags(). Va trebui să o plasezi în fișierul single-post.php
(sau single.php
dacă tema ta nu are un fișier single-post.php
) (pentru a găsi fișierul șablon corect poți consulta întotdeauna Ierarhia șabloanelor Wordpress).
Pentru a afișa o listă de etichete cu funcția menționată mai sus, ai nevoie de ceva similar cu:
<?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>

Iată o modalitate de a adăuga etichete după conținut doar pe articolele individuale folosind filtrul the_content
într-o funcție personalizată din fișierul functions. Folosește the_tags
function tags_after_single_post_content($content) {
if( is_singular('post') && is_main_query() ) {
$tags = the_tags('<div class="entry-meta">Etichetat cu: ',' • ','</div><br />');
$content .= $content . $tags;
}
return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );
Rezultat:

Cod corect:
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 .= 'Etichete: ' . implode(', ', $array) . '<br>';
}
return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );
Motivul pentru care acest răspuns este greșit este pentru că the_tags ar trebui folosit în interiorul The Loop și the_tags nu returnează nimic, astfel încât restul codului nu face nimic. În acest răspuns, get_the_tags returnează un array de instanțe de etichete, astfel încât le putem adăuga la conținut.

Afișează o listă de etichete cu linkuri către fiecare și o clasă specifică pentru fiecare etichetă:
$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='Eticheta {$tag->name}' class='{$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;

Trebuie să utilizați acest cod la sfârșitul fișierului 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 .= 'Etichete: ' . implode(', ', $array) . '<br>';
}
return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );

am făcut acest cod de la arterm cu mici modificări
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">Etichete: ' . implode(', ', $array) . '</div>';
}
return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );
