
用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 AI助手:开源免费的AI插件,为WordPress网站注入智能内容创作能力
WordPress ai助手:为 WordPress 用户提供集成多种 AI 模型的功能
wordpress获取当前文章最后一个tags标签(不带链接)
上面是“wordpress文章页面显示标签代码介绍(标签函数使用方法)”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_2386.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

css各种cursor鼠标样式介绍
Agnes AI:实现多人实时编辑文档、报告或PPT制作
2023年程序猿如何给自己开启一场烟花盛会