2024年05月30日 建站教程
用wordpress做网站,文章页面如何调用当前页面标签,下面web建站小编给大家介绍一下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 );
}
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>
本文链接:http://so.lmcjl.com/news/5627/