Home > WordPress > WordPress custom field之header应用

WordPress custom field之header应用

Custom field 自定义字段.wordpress很有用的一个功能,但是很多人却忽略了.今天介绍如何利用它做header部分的SEO优化.

关于自定义字段官方的解释是:
WordPress has the ability to allow post authors to assign custom fields to a post. This arbitrary extra information is known as meta-data. This meta-data can include bits of information

其实简单的理解.自定义字段就是容许作者自己定义特定的字段.通过 the_meta()函数在文章中获取所自定义的内容. 你可以理解为.这个自定义字段是你发布文章时额外附加于这篇文章的一些信息.

利用自定义字段完善header部分SEO优化.这里的header部分主要是指keywords和description.关于它的优化在去年的文章中介绍过.主要是参照了水煮鱼(鱼叔)的做法.但是我修正了肯造成的乱码问题.有兴趣的可以看看.

用于header的SEO光知道the_meta()这个函数是不行的.你还需要了解get_post_meta($post_id, $key, $single);它的具体信息建议到官方查看.我这里用到的是get_post_meta($post->ID, ‘Keywords’, true).将$single参数设置为true的作用是只返回自定义字段中的第一个数组.

因此我们在获得keywords的时候可以这么做:(当然,在后台,你要先建立一个Keywords的自定义字段)

$post_keywords = get_post_meta($post->ID, 'Keywords', true);
$keywords[] = $post_keywords;//加入到数组里面
//你的其他的处理
......
//把keywords输出来.
$keywords = join(', ', $keywords);//把数组里面的值都拿出来.用','分开.
echo '<meta name="keywords" content="' . wp_specialchars($keywords, 1) . '" />'

关于函数wp_specialchars()我自己还有些疑问,但据说是用用于防止攻击的.

关于描述(description)是同样的道理.Google比较注重Description.所以很有必要做些相应的优化. 但是需要注意一个问题,对于单篇文章或者页面.有时候我们设置了密码保护.只容许特定的人看.那就应该对搜索引擎隐藏这些内容.具体是做法是多加一个判断’$post->post_password’是否为空.这个我的主题中一直没有注意到的地方.今天才得以修正.! 汗颜~我的几篇加密文章.暴露了一点.

讲的比较笼统,只是一种思路.是在主题code或者修改时需要注意的地方.希望能对大家有所启发.如果你看的云里雾里.而你又需要更详尽的了解这方面的内容.我建议你下载我之前发布的主题.看看app目录下的yinheli_seo.php文件.我把Header部分以及相关的seo内容写在里面了.当然关于描述部分的考虑周,我将会在下个版本修复.

  1. ⌒_⌒TryLife Apr 8th, 2009 @ 17:28 | #1

    沙发

  2. yinheli Apr 8th, 2009 @ 17:29 | #2

    @⌒_⌒TryLife
    你的名字太长了. :mad:

  3. ⌒_⌒TryLife Apr 8th, 2009 @ 17:31 | #3

    沙发

    @yinheli
    不错在你这里正好 一个笑脸 一个名字 嘎嘎 :mrgreen:

  4. 会律博客 Apr 8th, 2009 @ 17:45 | #4

    这里的沙发也很热门的说,嘿嘿!

  5. ⌒_⌒TryLife Apr 8th, 2009 @ 18:01 | #5

    @会律博客
    是沙发 不写上沙发很浪费 嘎嘎

  6. toms Apr 8th, 2009 @ 18:07 | #6

    赞一个,研究的越来越深入了。以前用过你的win7那个主题,做的不错。加油。

  7. 任平生 Apr 8th, 2009 @ 20:58 | #7

    你 blog 模块左下角的设计让我以为这些模块是可以拖拉缩放的,害我不停地去拉啊拉。。。

  8. 任平生 Apr 8th, 2009 @ 21:01 | #8

    任平生 :
    你 blog 模块左下角的设计让我以为这些模块是可以拖拉缩放的,害我不停地去拉啊拉。。。

    哦,纠正一下,是左上角,右下角

  9. welee Apr 8th, 2009 @ 21:34 | #9

    Custom Field 很好用,除了 Header 的 Keyword,还可以用在首页文章显示缩略图,而像 postview 这个插件,也用到了 Custom Field。

  10. Leeiio Apr 8th, 2009 @ 21:37 | #10

    @任平生
    和我一個毛病~

  11. yinheli Apr 8th, 2009 @ 23:08 | #11

    @任平生
    还没有到那么高级的程度.

    @welee
    是的,我的主题是目前支持关键字和描述

  12. Showfom Apr 9th, 2009 @ 10:25 | #12

    All in One SEO就有自己的字段

  13. grubylee Apr 9th, 2009 @ 13:46 | #13

    Blog Friends wall 比较爽额,看起来人气比较旺啊,呵

  14. 吖Bee Apr 9th, 2009 @ 22:38 | #14

    呵呵~貌似正在使用中~改了一点点

  15. Showfom Apr 10th, 2009 @ 18:47 | #15

    我来看一下你这个猪头,哼! :razz:

  16. xiao3 Apr 10th, 2009 @ 21:18 | #16

    我想问下首页源代码里面的JS可以放到foot下面吗?要是可以,该怎么放呢?

  17. 吖Bee Apr 10th, 2009 @ 22:34 | #17

    来游行一下~~ :smile:

  18. yinheli Apr 11th, 2009 @ 01:10 | #18

    @xiao3
    如果是我的主题.js是在后面加载的.想调整也行.你复制一下换个位置就好了.记得jQuery要比其他js先加载.

    @吖Bee
    你是这里留言最多的家伙.边侧栏.和链接里有你的头像了.

  19. Mayki Apr 11th, 2009 @ 06:34 | #19

    Hello,
    I have a problem with your theme Philna Windows Seven, when I click on the news, my ereste blue screen, large as it nya blue background appears but no information …
    Can you help me please
    my version is the Worpress WordPress 2.7.1

  20. xiao3 Apr 11th, 2009 @ 08:31 | #20

    要是将JS放到foot。那我要怎么调用呢?就只是将JS剪切过去吗?不用添加其他的?因为我的侧边栏有JS要加载,所以老导致页面文章加载要受到侧栏影响,所以想调整下JS加载顺序!

  21. 吖Bee Apr 11th, 2009 @ 12:06 | #21

    Blog Friends (hot)这个不错呀~~还有其他小地方的改动~太有趣了! :eek:

  22. 蜡果 Apr 11th, 2009 @ 16:43 | #22

    不错。。。

  23. yinheli Apr 11th, 2009 @ 18:53 | #23

    @xiao3
    应该是直接剪切过去就行的.只需要注意jQuery框架的加载要在其他前面.

    @吖Bee
    其实是不想浪费已经缓存了的热情评论员的数据.所以多多的利用一下.

  24. 任平生 Apr 12th, 2009 @ 22:26 | #24

    @yinheli 哈哈,这个设计有误导的嫌疑哈~ :eek:

  25. hoho Apr 20th, 2010 @ 11:25 | #25

    你v2版本的这个主题怎么加入自定义字段啊?我折腾了一晚上也没搞出来。
    例如 我要在文章后边加入“该产品编号:……”自定义字段为number
    这个该在主题哪个文档中插入代码?无论header还是别的 试过了都不行
    希望作者看到了给回复一下 有回复通知请发到我的email

Submitting Comment, Give me a second...

Leave a comment

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
Trackbacks & Pingbacks ( 0 )
  1. No trackbacks yet.