分类: note

  • WordPress 更换域名必备 SQL 语句

    比如要将旧域名 kwnsn.com 改为 ygs.im/ ,可以按一下格式对号入座,更换域名就变得非常方便了,可以每天更换不同的域名了~囧

    UPDATE kwnsn_options SET option_value = replace( option_value, 'http://kwnsn.com', 'http://ygs.im' ) WHERE option_name = 'home' OR option_name = 'siteurl';
    UPDATE kwnsn_posts SET post_content = replace( post_content, 'http://kwnsn.com', 'http://ygs.im' ) ;
    UPDATE kwnsn_posts SET guid = replace( guid, 'http://kwnsn.com', 'http://ygs.im' ) ;
  • 用代码打造完美的“当前位置”

    之前介绍了两个显示当前位置的方式,其实就是两个不同的函数 get_the_category() 和 the_category() ,折腾出真知,单独使用两个函数都不尽人意:单独使用 get_the_category() ,如果文章属于两个分类,则文章页面只显示一个分类;单独使用 the_category() ,当某两个分类被同一篇文章“属于了”(能明白吧),那么在其中一个分类页面中同时显示两个分类,神奇。

    不过不用害怕,两者结合就能完美了,精简了下,只在分类和文章页面显示,代码如下,按需修改~

    <div class="localtion">
    	<?php if  ( is_category() ) { ?>
    		<p class="local">
    		你的位置: <a href="http://ygs.im/" title="HOME">HOME</a> >> <?php $categorys = get_the_category(); $category = $categorys[0];echo(get_category_parents($category->term_id,true,' >>')); ?> 文章列表
    		</p>
    	<?php } ?>
    	<?php if  ( is_single() ) { ?>
    		<p class="local">
    		你的位置: <a href="http://ygs.im/" title="HOME">HOME</a> >> <?php the_category(' & '); ?> >> <?php the_title(); ?>
    		</p>
    	<?php } ?>
    </div>
  • PHP substr 截取中文乱码的解决方法

    文章页面的 description 是使用 substr 函数来截取220字符的,但是最后一个汉字总是乱码,而且截取出来的长度也不正确。

    通过神奇的 Google 找到方法,可能是因为 substr(string,start,length),会将汉字以字符的形式截断,而造成乱码

    解决方案:使用 PHP 扩展库中的 mb_substr 方法。

    方法定义:
    string mb_substr ( string str, int start [, int length [, string encoding]] )

    注意:在使用 mb_substr()/mb_strcut 最后要加入多一个参数,以设定字符串的编码,

    例如:
    echo mb_substr(‘原本会出现乱码的汉字!’, 0, 7, ‘utf-8’);

    再如:
    $description = mb_substr(strip_tags($post->post_content),0,220,’utf-8′);

  • 一日一折腾之 WordPress SEO 优化

    哈哈,参照 WordPress SEO 技巧 把博客给优化优化,受益匪浅,匪浅啊。

    折腾也挺多的,分开来说。

    1.标题优化
    title 使用文中的方法,代码如下:

    <title><?php if ( is_single() || is_page() || is_category() || is_tag() ) { wp_title(''); } else { bloginfo('name'); } ?></title>

    去掉标题前的预留空格,代码放进模板 function.php ,格式为 <?php 代码 ?> :

    function titledespacer($title) {
    	return trim($title);
    }
    add_filter('wp_title', 'titledespacer');

    2.为 Read more 加上 nofollow
    方法跟上面的一样,也是加到 function.php。

    add_filter('the_content_more_link','nofollowReadMore' ,0);
    function nofollowReadMore($link) {
    	return str_replace('class="more-link"', 'class="more-link" rel="nofollow"', $link);
    }

    3.Keywords & Description
    类似的插件很多,我只用过 Simple Tags,觉得不太如意。后来移植了某个主题的代码,再后来在某个博客看到的代码,现在是根据上文的技巧改写的代码,只是还没完成页面的 Description ,加上我只有‘关于’一个页面,那就暂时忽略了,呵呵。经过折腾,代码已基本完美了,分类页的 description 是分类描述,要后台添加。完整代码,放到 head 中:

    <?php if (is_home()){
    $description = "Ygs' blog以民间折腾为主,个人生活为辅,前端设计为目的的个人博客";
    $keywords = "YGS,WordPress,前端设计,交互设计,用户体验";
    } elseif (is_single()){
    if ($post->post_excerpt) {
    $description = $post->post_excerpt;
    } else {
    $description = mb_substr(strip_tags($post->post_content),0,220,'utf-8');
    $description = str_replace(array("rn", "r", "n"," ","	"), " ", $description);
    }
    $keywords = "";
    $tags = wp_get_post_tags($post->ID);
    foreach ($tags as $tag ){
    $keywords = $keywords . $tag->name . ", ";
    }$keywords = substr($keywords,0,-2);
    } elseif(is_category()){
    $description = strip_tags(category_description());
    } elseif(is_tag()){
    $description = "";
    $tags = wp_get_post_tags($post->ID);
    foreach ($tags as $tag ){
    $description = $description . $tag->name . ", ";
    }$description = substr($description,0,-2);
    } elseif(is_page()){
    $description = $post->post_title ;
    } else {
    $description = "";}?>
    <?php if (is_single()||is_home()) {?>
    <meta name="keywords" content="<?=$keywords?>" />
    <meta name="description" content="<?=$description?>" />
    <?php } ?>
    <?php if (!is_single()&&!is_home()) {?>
    <meta name="keywords" content="" />
    <meta name="description" content="<?=$description?>" />
    <?php } ?>

    发现最后的 elseif 不会用,只能分开两个判断语句,效果一样,囧。
    此代码基本符合以下规则:

    页面类型KeywordsDescription
    首页自定义 keywords自定义 description
    文章页面标签组合摘要或者文章前 220 个字符
    (截取文章需要特殊处理全角字符)
    搜索页面搜索关键字
    分类存档页面分类
    标签存档页面标签
    日期存档页面日期
    其他页面页面标题

    4.将 Related Post 改为 More posts about XXX
    也就是将“相关文章”改为“与XX有关的文章”,我使用了文章的标签代替XX,具体模板具体分析
    获取标签函数 the_tags(”, ‘, ‘, ”);

    基本就这么多了~使用愉快~

  • jQuery 美妙的标题提示

    该特效“不但可以让你的 title 提示效果变得美观,而且可以显示出你将要点击的链接的 url,让用户知道自己将要去哪里”,明显提升了用户感受,至少也吸引了眼球。

    jQuery代码,另存为JS或者整合:

    jQuery(document).ready(function($){
    $("a").mouseover(function(e){
    	this.myTitle = this.title;
    	this.myHref = this.href;
    	this.myHref = (this.myHref.length > 30 ? this.myHref.toString().substring(0,30)+"..." : this.myHref);
    	this.title = "";
    	var tooltip = "<div id='tooltip'><p>"+this.myTitle+"<em>"+this.myHref+"</em>"+"</p></div>";
    	$('body').append(tooltip);
    	$('#tooltip').css({"opacity":"0.8","top":(e.pageY+20)+"px","left":(e.pageX+10)+"px"}).show('fast');
    }).mouseout(function(){this.title = this.myTitle;$('#tooltip').remove();
    }).mousemove(function(e){$('#tooltip').css({"top":(e.pageY+20)+"px","left":(e.pageX+10)+"px"});
    });
    });

    CSS美化代码:

    #tooltip {position:absolute;z-index:1000;max-width:250px;word-wrap:break-word;background:#000;text-align:left;padding:5px;min-height:1em;-moz-border-radius:5px;-khtml-border-radius:5px;-webkit-border-radius:5px;border-radius:5px;}
    #tooltip p {color:#fff;font:12px 'Microsoft YaHei',Arial,宋体,Tahoma,Sans-Serif;}
    #tooltip p em {display:block;margin-top:3px;color:#f60;font-style:normal;}

    最后,别忘了载入jQuery库。

  • 今日大动作

    首先,把目录移到上方啦,把分类精简啦,那些只懂一点而且不会深入发展的就去掉啦,里面的文章就移到了“默认分类”啦。

    然后,侧边栏加上评论的调用啦。

    最后,好像没有了吧。

    通过搜索引擎进入的竟然一个都没有,看来前几天的域名折腾影响颇大,颇大呀。不过呢,作为开闭站专业户,算是家常便饭啦。嗯,周末应该去玩水了吧,如果没有台风的话。

  • WordPress 调用最新评论

    你的模板没有评论栏目?可以使用边栏小工具添加,但是使用了边栏工具对于不同页面显示不同栏目不好控制,所以我并没有使用,又所以使用了自己添加代码的方法,代码如下:

    <?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 = '' AND user_id != '1'
    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="" .
    $comment->post_title . "">" . strip_tags($comment->com_excerpt)
    ."</a></li>";
    }
    $output .= $post_HTML;
    echo $output;
    ?>
    

    其中 user_id != ‘1’ 的功能为隐藏管理员(ID=1)的评论。

  • 域名小折腾:301重定向

    今年初一时冲动注册了 kwnsn.com 这个域名,其实还不错了,但是不知道 kwnsn 的意思有点难记住。其实 kwnsn 是我名字粤语拼音的缩写:) 一直考虑要不要换域名,又觉得多数流量都是通过搜索引擎进入的,也不会记你的域名,所以一直没理了。今天突然就注册了 y-gs.me ,而且已经在使用了…说不清,先使用一段时间吧~善变也是我的特色。

    顺便说说我使用 WordPress 更换域名的步骤。因为使用的是 cPanel ,操作起来挺方便了。首先是将网站的文件复制到 y-gs.me ,绑定新域名,因为没有更换数据库,所以只需通过旧域名登陆站点后台更改站点地址和安装地址为新的域名即可。

    最后就是将旧域名作 301 重定向到新网址了:

    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

    这是原来的 .htaccess 文件的代码,要将其删除,不然你输入 kwnsn.com/about 还是会进入到 kwnsn.com/about 而不是 y-gs.me/about 。

    删除以上代码后,就要加上以下代码了,作用是将旧域名重定向到新域名

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^kwnsn.com [NC]
    RewriteRule ^(.*)$ http://ygs.im/$1 [L,R=301]

    好,这样还没折腾完,别忘了修改模板中的域名,广告联盟的域名,百度联盟的域名…

  • 工具:文本查找替换专家

    小工具大功能,之前修改模板用它很快就能找到该修改那些地方,推荐使用。

    详细说明:文本替换专家,使用简单,功能强大,支持多级目录同时替换,支持大小写匹配,支持文件备份,支持文件查找,智能历史替换方案记录以及智能备份文件批量还原系统,让文本查找、批量查找、替换、批量替换、重复查找替换及批量备份还原更轻松,自带简洁高效文本编辑功能,让你的文本修改更加得心应手,高效跟踪引擎以及完善的操作报表,让操作结果清晰明了。程序特有的文件检索引擎,使文件检索速度极快。程序小巧,绿色免费,任君随意使用。

  • JavaScript 的 Cookie 控制

    这是当年看着书学习修改的代码,用来控制banner的显示与否。

    原理:正常情况下显示 banner,当点击关闭按钮时,通过 turnoff(obj); 函数添加一条cookie: guide_status=guide_close ,并设置 CSS display 为 none。通过 readcookie(the_info); 函数读取 cookie ,判断 guide_status=guide_close 是否存在,存在即设置 CSS display 为 none。

    函数:

    turnoff(obj);
    function turnoff(obj){
    //设置cookie过期时间,这里设为2012发生日
    var the_date=new Date("December 21, 2012");
    var the_cookie_date=the_date.toUTCString();
    //写入cookie+过期时间
    document.cookie="guide_status=guide_close;expires="+the_cookie_date;
    //隐藏banner
    document.getElementById(obj).style.display="none";
    }
    

    该函数以 onclick 事件插入到关闭按钮中: onclick=”javascript:turnoff(‘guidebar’)” ,guidebar 为 banner ID。

    (更多…)