效果图
插件说明:
- 插件可实现自动检测文章中出现的已有标签列表中词语,并自动为标签词语添加所在链接,增加站内链接以利于SEO。
- 插件可实现为标签添加class属性,由主题CSS设置样式(如需字体加粗,边框等等)。
- 插件可实现为标签统一设置颜色。
- 插件可设置最低词频,检测文章中存在多个相同标签时,将前N个标签自动增加链接。
wordpress网站做久了以后,标签tag也会积累得越来越多,为了方便统计和浏览,我们可以给tag加上标签云,在前台显示出来,今天西瓜君就带大家一起来看看吧。
我们可以用wp_tag_cloud()这个wordpress的默认函数来调用标签云,下面的代码可以实现:
<?php wp_tag_cloud('smallest=12&largest=18&unit=px&number=0&orderby=count&order=DESC');?>
smallest表示标签的最小字号
largest表示最大字号
unit=px表示字体使用像素单位
number=0表示显示所有标签,如果为40,表示显示40个
orderby=count表示按照标签所关联的文章数来排列
order=DESC表示降序排序(ASC表示升序排序,DESC表示降序排序)
更多 wp_tag_cloud() 参数,请参考 WordPress文档 wp tag cloud
我们通过上面的代码,已经可以调用表群晕了,然后我们再把下面的代码放进主题的functions.php
文件的最后:
//彩色标签云
function colorCloud($text) {
$text = preg_replace_callback('|<a (.+?)>|i','colorCloudCallback', $text);
return $text;
}
function colorCloudCallback($matches) {
$text = $matches[1];
$color = dechex(rand(0,16777215));
$pattern = '/style=('|”)(.*)('|”)/i';
$text = preg_replace($pattern, "style="color:#{$color};$2;"", $text);
return "<a $text>";
}
add_filter('wp_tag_cloud', 'colorCloud', 1);
你可以用下面的函数来调用标签云,但是具体怎么修改,只能自己研究了,因为wordpress每个主题都不一样:
<?php wp_tag_cloud('smallest=12&largest=18&unit=px&number=20');?>
国内大部分主题都是支持边栏小工具的,你可以先看看小工具里有没有彩色标签云,如果有的话,你可以忽略上面所有步骤,直接添加就好。
[wm_tips]文本元素是用来在应用窗口中显示一段文字(字符 文本)[/wm_tips]
1、创建文本元素
// new PIXI.Text(); :创建文本元素
// "得分:10000" : 文本元素中的内容(文字、字符)
// var score : 文本元素的别名
var score = new PIXI.Text("得分:10000");
2、在应用窗口中添加文本元素
//在名为app的应用窗口中(stage),添加一个元素(addChild),元素名为 score
app.stage.addChild( score );
[wm_notice]注意:文本元素中的默认字体颜色为黑色[/wm_notice]
[wm_tips]属性:可以理解为配置,我们可以自定义属性的值来修改原有的配置[/wm_tips]
//score : 文本元素的名称
//style : 风格
//fill : 填充颜色
// = "green" :绿色
//修改文本元素的属性,将颜色改为白色
score.style.fill = "green";
[wm_notice]我们可以将“ ”号中的颜色改为自己需要的颜色[/wm_notice]
[wm_tips]十六进制颜色编码:为每种颜色匹配的一个编号,不需要背,当需要使用某种颜色时,可以从百 度中直接输入十六进制颜色编码表,找到自己需要 的颜色 编码[/wm_tips]
// "#cff060" :就表示了一种颜色
score.style.fill = "#cff060"
[wm_notice]在应用窗口中元素默认的位置实在左上角(原点),可以通过修改定位点来改变元素的位置 定位点由两部分构成,分别是x坐标点与y坐标点[/wm_notice]
[wm_red]
1.应用窗口的宽度为500px,高度为400px,以左上角为原点向下延伸属于y轴,向右延伸属于x轴,
2.元素越靠右x的值越大,元素越靠下y的值越大
3.设置元素的x坐标与y坐标来定义元素在窗口中的位置
[/wm_red]
//设置文本元素位置
元素别名.x = 340;//设置x坐标为340
元素别名.y = 20;// 设置y坐标为20
调整后效果
<!-- 引入外部的pixi.min.js -->
<script src="../js/pixi.min.js"></script>
<script>
//1、创建应用窗口(宽 480 ,高 700)
var app = new PIXI.Application(480,700);
//2、在页面中添加应用窗口
document.body.appendChild( app.view );
//3、创建图片元素(背景图片)路径:img/bg.jpg
var bg = new PIXI.Sprite.fromImage("img/bg.jpg");
//4、在应用窗口中添加图片
app.stage.addChild( bg );
//5、创建图片元素(赛车1图片)路径:img/saiche-2_03.png
var c1 = new PIXI.Sprite.fromImage("img/saiche-2_03.png");
//6、设置图片元素位置:x = 165 , y = 275
c1.x = 165;
c1.y = 275;
//7、在应用窗口中添加图片
app.stage.addChild( c1 );
//8、创建图片元素(赛车2图片)路径:img/saiche-3_03.png
var c2 = new PIXI.Sprite.fromImage("img/saiche-3_03.png");
//9、设置图片元素位置:x = 255 , y = 575
c2.x = 255;
c2.y = 575;
//10、在应用窗口中添加图片
app.stage.addChild(c2);“
</script>
1、引用pixi.min.js
<script src="路径"></script>
2、加入script标签(将来在script标签中编码)
<script> </script>
3、创建应用窗口元素
var app = new PIXI.Application(宽,高);
4、在页面中添加应用窗口元素
document.body.appendChild( app.view );
5、根据需要创建元素(文本、图片)设置元素属性(颜色、位置)
6、在应用窗口中添加元素
app.stage.addChild( 元素名 );
[wm_warn]如果页面中由多个元素构成 5/6步,反复编写[/wm_warn]
[wm_warn]注意:
以下文件 可能和部分主题有冲突 修改前记得备份!!!
以下文件 可能和部分主题有冲突 修改前记得备份!!!
以下文件 可能和部分主题有冲突 修改前记得备份!!!
重要事情说三遍[/wm_warn]
style.css 样式表文件 index.php 主页文件 single.php 日志单页文件 page.php 页面文件 archvie.php 分类和日期存档页文件 searchform.php 搜索表单文件 search.php 搜索页面文件 comments.php 留言区域文件(包括留言列表和留言框) 404.php 404错误页面 header.php 网页头部文件 sidebar.php 网页侧边栏文件 footer.php 网页底部文件
<?php bloginfo(’name’); ?> 网站标题
<?php wp_title(); ?> 日志或页面标题
<?php bloginfo(’stylesheet_url’); ?> WordPress主题样式表文件style.css的相对地址
<?php bloginfo(’pingback_url’); ?> WordPress博客的Pingback地址
<?php bloginfo(’template_url’); ?> WordPress主题文件的相对地址
<?php bloginfo(’version’); ?> 博客的WordPress版本
<?php bloginfo(’atom_url’); ?> WordPress博客的Atom地址
<?php bloginfo(’rss2_url’); ?> WordPress博客的RSS2地址
<?php bloginfo(’url’); ?> WordPress博客的绝对地址
<?php bloginfo(’name’); ?> WordPress博客的名称
<?php bloginfo(’html_type’); ?> 网站的HTML版本
<?php bloginfo(’charset’); ?> 网站的字符编码格式
<?php the_content(); ?> 日志内容
<?php if(have_posts()) : ?> 确认是否有日志
<?php while(have_posts()) : the_post(); ?> 如果有,则显示全部日志
<?php endwhile; ?> 结束PHP函数”while”
<?php endif; ?> 结束PHP函数”if”
<?php get_header(); ?> header.php文件的内容
<?php get_sidebar(); ?> sidebar.php文件的内容
<?php get_footer(); ?> footer.php文件的内容
<?php the_time(’m-d-y’) ?> 显示格式为”02-19-08″的日期
<?php comments_popup_link(); ?> 显示一篇日志的留言链接
<?php the_title(); ?> 显示一篇日志或页面的标题
<?php the_permalink() ?> 显示一篇日志或页面的永久链接/URL地址
<?php the_category(’, ‘) ?> 显示一篇日志或页面的所属分类
<?php the_author(); ?> 显示一篇日志或页面的作者
<?php the_ID(); ?> 显示一篇日志或页面的ID
<?php edit_post_link(); ?> 显示一篇日志或页面的编辑链接
<?php get_links_list(); ?> 显示Blogroll中的链接
<?php comments_template(); ?> comments.php文件的内容
<?php wp_list_pages(); ?> 显示一份博客的页面列表
<?php wp_list_cats(); ?> 显示一份博客的分类列表
<?php next_post_link(’ %link ‘) ?> 下一篇日志的URL地址
<?php previous_post_link(’%link’) ?> 上一篇日志的URL地址
<?php get_calendar(); ?> 调用日历
<?php wp_get_archives() ?> 显示一份博客的日期存档列表
<?php posts_nav_link(); ?> 显示较新日志链接(上一页)和较旧日志链接(下一页)
<?php bloginfo(’description’); ?> 显示博客的描述信息
/%postname%/ 显示博客的自定义永久链接
<?php the_search_query(); ?> 搜索表单的值
<?php _e(’Message’); ?> 打印输出信息
<?php wp_register(); ?> 显示注册链接
<?php wp_loginout(); ?> 显示登入/登出链接
<!–next page–> 在日志或页面中插入分页
<!–more–> 截断日志
<?php wp_meta(); ?> 显示管理员的相关控制信息
<?php timer_stop(1); ?> 显示载入页面的时间
<?php echo get_num_queries(); ?> 显示载入页面查询
WordPress最新文章的调用可以使用一行很简单的模板标签wp_get_archvies来实现. 代码如下:
<?php get_archives(‘postbypost’, 10); ?> (显示10篇最新更新文章)
或者
<?php wp_get_archives(‘type=postbypost&limit=20&format=custom’); ?>
后面这个代码显示你博客中最新的20篇文章,其中format=custom这里主要用来自定义这份文章列表的显示样式。具体的参数和使用方法你可 以参考官方的使用说明- wp_get_archvies。(fromat=custom也可以不要,默认以UL列表显示文章标题。)
补充: 通过WP的query_posts()函数也能调用最新文章列表, 虽然代码会比较多一点,但可以更好的控制Loop的显示,比如你可以设置是否显示摘要。具体的使用方法也可以查看官方的说明。
<?php
$rand_posts = get_posts(‘numberposts=10&orderby=rand’);
foreach( $rand_posts as $post ) :
?>
<!–下面是你想自定义的Loop–>
<li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
<?php endforeach; ?>
<?php
global $wpdb;
$sql = “SELECT DISTINCT ID, post_title, post_password, comment_ID,
comment_post_ID, comment_author, comment_date_gmt, comment_approved,
comment_type,comment_author_url,
SUBSTRING(comment_content,1,30) AS com_excerpt
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
$wpdb->posts.ID)
WHERE comment_approved = ’1′ AND comment_type = ” AND
post_password = ”
ORDER BY comment_date_gmt DESC
LIMIT 10″;
$comments = $wpdb->get_results($sql);
$output = $pre_HTML; foreach ($comments as $comment) {
$output .= “n<li>”.strip_tags($comment->comment_author)
.”:” . ” <a href=”" . get_permalink($comment->ID) .
“#comment-” . $comment->comment_ID . “” title=”on ” .
$comment->post_title . “”>” . strip_tags($comment->com_excerpt)
.”</a></li>”;
} $output .= $post_HTML;
echo $output;?>
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
‘tag__in’ => array($first_tag),
‘post__not_in’ => array($post->ID),
‘showposts’=>10,
‘caller_get_posts’=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>”><?php the_title();?> <?php comments_number(‘ ‘,’(1)’,'(%)’); ?></a></li>
<?php
endwhile;
}
}
wp_reset_query();
?>
<?php $posts = get_posts( “category=4&numberposts=10″ ); ?>
<?php if( $posts ) : ?>
<ul><?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<li>
<a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php
global $wpdb;
$sql = “SELECT DISTINCT ID, post_title, post_password, comment_ID,
comment_post_ID, comment_author, comment_date_gmt, comment_approved,
comment_type,comment_author_url,
SUBSTRING(comment_content,1,14) AS com_excerpt
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
$wpdb->posts.ID)
WHERE comment_approved = ’1′ AND comment_type = ” AND
post_password = ”
ORDER BY comment_date_gmt DESC
LIMIT 10″;
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {
$output .= “\n<li>”.strip_tags($comment->comment_author)
.”:” . ” <a href=\”" . get_permalink($comment->ID) .
“#comment-” . $comment->comment_ID . “\” title=\”on ” .
$comment->post_title . “\”>” . strip_tags($comment->com_excerpt)
.”</a></li>”;
}
$output .= $post_HTML;
echo $output;?>
<?php
global $wpdb;
$sql = “SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_author_email, comment_type,comment_author_url, SUBSTRING(comment_content,1,10) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = ’1′ AND comment_type = ” AND comment_author != ‘郑 永’ AND post_password = ” ORDER BY comment_date_gmt DESC LIMIT 10″;
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {
$output .= “\n<li>”.get_avatar(get_comment_author_email(‘comment_author_email’), 18). ” <a href=\”" . get_permalink($comment->ID) . “#comment-” . $comment->comment_ID . “\” title=\”" . $comment->post_title . ” 上的评论\”>”. strip_tags($comment->comment_author) .”: “. strip_tags($comment->com_excerpt) .”</a></li>”;
}
$output .= $post_HTML;
$output = convert_smilies($output);
echo $output;
?>
[wm_notice]上面代码把comment_author的值改成你的ID,18是头像大小,10是评论数量。[/wm_notice]
1.日志总数
<?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish;?>
2.草稿数目
<?php $count_posts = wp_count_posts(); echo $draft_posts = $count_posts->draft; ?>
3.评论总数
<?php echo $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->comments”);?>
4.成立时间
<?php echo floor((time()-strtotime(“2008-8-18″))/86400); ?>
5.标签总数
<?php echo $count_tags = wp_count_terms(‘post_tag’); ?
6.页面总数
<?php $count_pages = wp_count_posts(‘page’); echo $page_posts = $count_pages->publish; ?>
7.分类总数
<?php echo $count_categories = wp_count_terms(‘category’); ?>
8.链接总数
<?php $link = $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = ‘Y’”); echo $link; ?>
9.用户总数
<?php $users = $wpdb->get_var(“SELECT COUNT(ID) FROM $wpdb->users”); echo $users; ?>
10.最后更新
<?php $last = $wpdb->get_results(“SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = ‘post’ OR post_type = ‘page’) AND (post_status = ‘publish’ OR post_status = ‘private’)”);$last = date(‘Y-n-j’, strtotime($last[0]->MAX_m));echo $last; ?>
判断是否是具体文章的页面
is_single()
判断是否是具体文章(id=2)的页面
is_single(’2′)
判断是否是具体文章(标题判断)的页面
is_single(’Beef Stew’)
判断是否是具体文章(slug判断)的页面
is_single(’beef-stew’)
是否留言开启
comments_open()
是否开启ping
pings_open()
是否是页面
is_page()
id判断,即是否是id为42的页面
is_page(’42′)
判断标题
is_page(’About Me’)
slug判断
is_page(’about-me’)
是否是分类
is_category()
id判断,即是否是id为6的分类
is_category(’6′)
分类title判断
is_category(’Cheeses’)
分类 slug判断
is_category(’cheeses’)
判断当前的文章是否属于分类5
in_category(’5′)
将所有的作者的页面显示出来
is_author()
显示author number为1337的页面
is_author(’1337′)
通过昵称来显示当前作者的页面
is_author(’Elite Hacker’)
下面是通过不同的判断实现以年、月、日、时间等方式来显示归档
is_date()
is_year()
is_month()
is_day()
is_time()
判断当前是否是归档页面
is_archive()
判断是否是搜索
is_search()
判断页面是否404
is_404()
判断是否翻页,比如你当前的blog是https://www.dujia520.cn显https://www.dujia520.cn?paged=2的时候,这个判断将返 回真,通过这个函数可以配合is_home来控制某些只能在首页显示的界面,
例如:
<?php if(is_single()):?>
//这里写你想显示的内容,包括函数
<?php endif;?>
或者
<?php if(is_home() && !is_paged() ):?>
//这里写你想显示的内容,包括函数
<?php endif;?>
<!–smilies–>
<?php
function wp_smilies() {
global $wpsmiliestrans;
if ( !get_option(‘use_smilies’) or (empty($wpsmiliestrans))) return;
$smilies = array_unique($wpsmiliestrans);
$link=”;
foreach ($smilies as $key => $smile) {
$file = get_bloginfo(‘wpurl’).’/wp-includes/images/smilies/’.$smile;
$value = ” “.$key.” “;
$img = “<img src=\”{$file}\” alt=\”{$smile}\” />”;
$imglink = htmlspecialchars($img);
$link .= “<a href=\”#commentform\” title=\”{$smile}\” οnclick=\”document.getElementById(‘comment’).value += ‘{$value}’\”>{$img}</a> ”;
}
echo ‘<div>’.$link.’</div>’;
}
?>
<?php wp_smilies();?>
<!–smilies—>
[wm_notice]将以上代码复制到 comments.php 中合适的位置即可(修改前记得备份,以上文件 可能和部分主题有冲突)[/wm_notice]
wordpress每次都要手动给文章添加标签,很麻烦?不知文章是否出现以前用过的标签,怎么办?以下代码就可以解决这些问题,它会在你发布/保存文章时,检测文章的内容中,是否出现曾经使用过的标签,如果出现,就自动为文章添加这些标签。
[wm_notice]以下有三个版本自行选择[/wm_notice]
[wm_blue]将代码添加到主题的 functions.php 即可[/wm_blue]
代码
/** * WordPress 自动为文章添加已使用过的标签 * https://www.wpdaxue.com/auto-add-tags.html */ add_action('save_post', 'auto_add_tags'); function auto_add_tags(){ $tags = get_tags( array('hide_empty' => false) ); $post_id = get_the_ID(); $post_content = get_post($post_id)->post_content; if ($tags) { foreach ( $tags as $tag ) { // 如果文章内容出现了已使用过的标签,自动添加这些标签 if ( strpos($post_content, $tag->name) !== false) wp_set_post_tags( $post_id, $tag->name, true ); } } }
[wm_blue]修改后可控制标签输出数量[/wm_blue]
代码
// WordPress 自动为文章添加已使用过的标签 add_action('save_post', 'auto_add_tags'); function auto_add_tags(){ $tags = get_tags( array('hide_empty' => false) ); $post_id = get_the_ID(); $post_content = get_post($post_id)->post_content; if ($tags) { $i = 0; foreach ( $tags as $tag ) { // 如果文章内容出现了已使用过的标签,自动添加这些标签 if ( strpos($post_content, $tag->name) !== false){ if ($i == 5) { // 控制输出数量 break; } wp_set_post_tags( $post_id, $tag->name, true ); $i++; } } } }
[wm_xuk]但这样输出,会默认输出前几个标签,考虑到每次输出都是标签库里面的前几个标签,不利于 SEO ,增加了标签打乱功能。[/wm_xuk]
[wm_blue]标签打乱增强版[/wm_blue]
代码
// WordPress 自动为文章添加已使用过的标签 function array2object($array) { // 数组转对象 if (is_array($array)) { $obj = new StdClass(); foreach ($array as $key => $val){ $obj->$key = $val; } } else { $obj = $array; } return $obj; } function object2array($object) { // 对象转数组 if (is_object($object)) { foreach ($object as $key => $value) { $array[$key] = $value; } } else { $array = $object; } return $array; } add_action('save_post', 'auto_add_tags'); function auto_add_tags(){ $tags = get_tags( array('hide_empty' => false) ); $post_id = get_the_ID(); $post_content = get_post($post_id)->post_content; if ($tags) { $i = 0; $arrs = object2array($tags);shuffle($arrs);$tags = array2object($arrs);// 打乱顺序 foreach ( $tags as $tag ) { // 如果文章内容出现了已使用过的标签,自动添加这些标签 if ( strpos($post_content, $tag->name) !== false){ if ($i == 5) { // 控制输出数量 break; } wp_set_post_tags( $post_id, $tag->name, true ); $i++; } } } }