admin管理员组文章数量:1122833
I'm converting an HTML template into a wordpress theme and I'm having some trouble displaying the post tags within the functions.php file. What I would like to do is add the following code into the functions file with the HTML code. I have been trying for a few days now and I have already been to the codex but nothing I try is working.
<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>
Can anyone please help with this?
I'm converting an HTML template into a wordpress theme and I'm having some trouble displaying the post tags within the functions.php file. What I would like to do is add the following code into the functions file with the HTML code. I have been trying for a few days now and I have already been to the codex but nothing I try is working.
<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>
Can anyone please help with this?
Share Improve this question edited Aug 7, 2014 at 13:59 Brad Dalton 6,9672 gold badges36 silver badges47 bronze badges asked Jun 30, 2014 at 20:06 user1181153user1181153 311 gold badge3 silver badges6 bronze badges 3- Sorry, but you are not providing enough informations to give an clear answer. The functions.php file is supposed to include custom theme helper functions and no direct html output. Where do you exactly want the output to happen? You want to output a list of assigned tags on a singles post view? – s1lv3r Commented Jun 30, 2014 at 20:15
- 2 Sorry, yes, I want the output to be in the single post. The tags you assign to each a post. – user1181153 Commented Jun 30, 2014 at 20:21
- Through the functions file? Or through any file? – Brad Dalton Commented Aug 7, 2014 at 13:07
6 Answers
Reset to default 4I think you are searching for the get_tags() function. You would need to place it into the single-post.php
(or single.php
if your theme doesn't have a single-post.php
) (to find the right template file you can always look up at Wordpress Template Hierarchy).
To echo a list of tags with the aboved linked function you would need to use something like:
<?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>
Here's one way to add post tags after the content on single posts only using the_content
filter in a custom function from your functions file. Uses the_tags
function tags_after_single_post_content($content) {
if( is_singular('post') && is_main_query() ) {
$tags = the_tags('<div class="entry-meta">Tagged with: ',' • ','</div><br />');
$content .= $content . $tags;
}
return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );
Result:
Correct code:
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 .= 'Tags: ' . implode(', ', $array) . '<br>';
}
return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );
Reason this answer is wrong because the_tags is should be used inside The Loop and the_tags return nothing so other code is doing nothing. In this answer get_the_tags return array of tag instances so we can append them to content.
Displays a list of tags with links to each one and a specific class for each 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->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;
You must use this code in the end of functions.php file:
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 .= 'Tags: ' . implode(', ', $array) . '<br>';
}
return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );
i make this code from arterm with litle change
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">Tags: ' . implode(', ', $array) . '</div>';
}
return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );
本文标签: functionsHow to display post tags
版权声明:本文标题:functions - How to display post tags 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310403a1934363.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论