web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

wordpress文章页面显示标签代码介绍(标签函数使用方法)

598 ℃
     

wordpress做网站,文章页面如何调用当前页面标签,下面web建站小编给大家介绍一下get_the_tags的使用方法!

get_the_tags()函数介绍

get_the_tags( int $post_id )

//$post_id(int):(必填)文章的 ID。

tags标签调用方法在wp-includes/category-template.php

function get_the_tags( $post_id = 0 ) {
  $terms = get_the_terms( $post_id, 'post_tag' );
  return apply_filters( 'get_the_tags', $terms );
}

get_the_tags()函数使用介绍

1、输出当前文章第一个标签

$post_tags = get_the_tags();
if ( $post_tags ) {
  echo $post_tags[0]->name; 
}

2、输出当前文章所有标签

$post_tags = get_the_tags();
if ( $post_tags ) {
  foreach( $post_tags as $tag ) {
    echo $tag->name . ', '; 
  }
}

3、显示带有链接和自定义分隔符的文章标签

function show_tags(){
  $post_tags = get_the_tags();
  $separator = ' | ';
  if (!empty($post_tags)) {
    foreach ($post_tags as $tag) {
      $output .= '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a>' . $separator;
    }
    return trim($output, $separator);
  }
}

4、在下拉列表中显示文章的标签

<?php
function dropdown_tags(){
  echo '<select name="tag" id="tag" class="postform">';
  foreach ( get_the_tags() as $tag ) {
    echo '<option value="' . $tag->name . '">' . $tag->name . "</option>\n";
  }
  echo '</select>';
}
?>
<h2><?php _e( 'Tags:', 'textdomain' ); ?></h2>
<form id="tags-select" class="tags-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
<?php dropdown_tags(); ?>
<input type="submit" name="submit" value="view" />
</form>

wordpress获取当前文章最后一个tags标签(不带链接)

wordpress利用mysql让文章 ID重新排列

wordpress如何在搜索结果中排除指定分类的文章

wordpress如何设置自动发布文章功能

wordpress如何用代码自动清空回收站

标签: tags标签, wordpress

上面是“wordpress文章页面显示标签代码介绍(标签函数使用方法)”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_2386.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > wordpress
本文共计1158个字,预计阅读时长8分钟
生活小工具,收录了80多款小工具
上一篇: 推荐2款充满科技感的艺术字体——小新黑体、小新潮酷体
下一篇: 推荐一款浏览器数据库的开源工具库——Dexie.js(JavaScript调用)
x 打工人ai神器