experience

经验

zw-1988.com

home > 经验 > wordpress 获取当前文章的tag标签

2018.1.08 wordpress 获取当前文章的tag标签

zwnet

一下代码可以放在分类页面内使用

代码在段落中输出标签列表,标签之前是用逗号分开的。

<?php
echo get_the_tag_list('标签: ',', ','');
?>

 

检查文章是否含有任何标签,如果有,将标签以无序列表的形式输出。

<?php
if(get_the_tag_list()) {
echo get_the_tag_list('<ul><li>','</li><li>','</li></ul>');
}
?>

 

源文件

get_the_tag_list() 位于 wp-includes/category-template.php

 

这段代码只适合在single页面用

标签:<?php 
$tags = wp_get_post_tags($post->ID);
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>";
};
echo $html;?>

以上是输出的有连接

这个仅仅是字符

<?php
$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    echo $tag->name . ' '; 
  }
}
?>

输出

<?php
$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    echo "<span title='{$tag->name} Tag' class='{$tag->slug}'>{$tag->name}</span>"; 
  }
}
?>

用于自定义tag,输出文章所包含得tag和本身链接

<ul>       
<?php 
        $post_type = $post->post_type;
        $taxonomies = get_object_taxonomies( $post_type, 'objects' );
        $terms = get_the_terms( $post->ID, 'interview-tag' ); 
         if(!empty($terms) ){
        foreach ( $terms as $term ){ 
                            $termname = esc_html( $term->name );
                             $termlink = get_term_link($term);
            echo '<li><a  href="' . $termlink . '">' . $termname . '</a></li> ' ;}
                }
	?></ul>

 

«
相关文章
wordpress (25)java script (9)tool (8)css (2)攻略 (1)第一次人体 (1)插件 (1)ECharts (1)food (1)雪糕 (1)动画 (1)book (1)标签云 (1)生活 (1)一言难尽的画稿 (1)
辽ICP备16002031号

Copyright © 2016 - 2020 1988-zw.com