怎么在wordpress中自定义标签云与随机获取标签

发布时间:2021-05-18 17:09:08 作者:Leah
来源:亿速云 阅读:242

本篇文章为大家展示了怎么在wordpress中自定义标签云与随机获取标签,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

用法

<?php wp_tag_cloud( $args ); ?>

默认用法

<?php $args = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', 'exclude' => null, 'include' => null, 'topic_count_text_callback' => default_topic_count_text, 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true, 'child_of' => null(see Note!) ); ?>

注: child_of 不是一个直接的 wp_tag_cloud 数组的键(Key),但由于这个函数使用 wp_parse_args() get_terms() ,你可以通过 get_terms() 使用所有的数组键。

默认情况下的输出内容:

但由于该方法把样式集合到了里面,使用起来不怎么友好,如果想自定义读取标签并修改展示样式该怎么做呢,那也是非常简单的,看代码实例,这里根据get_tags来获取:

$html = '<ul class="post_tags">';
foreach (get_tags( array('number' => 50, 'orderby' => 'count', 'order' => 'DESC', 'hide_empty' => false) ) as $tag){
 $color = dechex(rand(0,16777215));
 $tag_link = get_tag_link($tag->term_id);
 $html .= "<li><a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}' style='color:#{$color}'>";
 $html .= "{$tag->name} ({$tag->count})</a></li>";
}
$html .= '</ul>';
echo $html;

如果要求随机获取标签在首页显示,那可以使用以下代码,但这种做法貌似不利于seo,可得慎重使用

//获取随机标签
function get_rand_tags()
{
 global $post, $wpdb;
 $sql = "SELECT * FROM {$wpdb->prefix}terms wt INNER JOIN {$wpdb->prefix}term_taxonomy wtt on wt.term_id=wtt.term_id where wtt.taxonomy='post_tag' ORDER BY RAND() LIMIT 20";
 $related_posts = $wpdb->get_results($sql);
 $html = '<ul class="post_tags">';
 foreach($related_posts as $tag)
 {
 $color = dechex(rand(0,16777215));
 $tag_link = get_tag_link($tag->term_id);
 $html .= "<li><a href='{$tag_link}' target='_blank' title='{$tag->name} Tag' class='{$tag->slug}' style='color:#{$color}'>";
 $html .= "{$tag->name} ({$tag->count})</a></li>";
 }
 $html .= '</ul>';
 echo $html;
}

上述内容就是怎么在wordpress中自定义标签云与随机获取标签,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 在python中实现随机选择
  2. 怎么在docker中安装wordpress

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

wordpress

上一篇:使用Yii2怎么处理密码加密及验证

下一篇:如何在php中生成标准的uuid

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》