今天的这篇文章为大家收集整理了14个WordPress自定义域的应用技巧。 WordPress之所以如此的流行与受人追捧,其中一个重要因素,我想,一定少不了“自定义域”这个强大的功能吧——竟然有人说WordPress的自定义域功能可以将一个博客彻底的改头换面!此话似乎说得有些夸张,不过也的确说明了其自定义威力之强猛,哈哈!
1. 设定文章显示时间期限
你有时候发表一篇文章,可能需要在某个时间里停止显示它?这听起来似乎有些难度,但是WordPress的自定义域却可以做到这一点。我们可以这样做,编辑当前主题中的loop循环,使用下面的代码替换原来的loop:
<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// For example...
the_title();
the_excerpt();
}
endwhile;
endif;
?>
创建一篇文章在一个预定的时间或日期之后过期,您所需要做的仅仅是自定义一个字段。指定一个关键字expiration作为日期及时间标识,其格式必须为 mm/dd/yyyy 00:00:00。过了这个时间,这篇文章就不再显示了。这段代码很简单, WordPress循环会自动查询是否存在自定义字段expiration,如果存在,则对比自定义时间与当前的时间。
代码来源: How to: Set a post’s expiration date and time on your WordPress blog
2. 设置文章在首页的显示方式
我们可以让文章在首页显示为全文,或仅显示摘要,通过自定义域就能够做到这一点。在主题的index.php文件中找到loop循环,然后用下面的代码来替换:
<?php if (have_posts()) :
while (have_posts()) : the_post();
$customField = get_post_custom_values("full");
if (isset($customField[0])) {
//Custom field is set, display a full post
the_title();
the_content();
} else {
// No custom field set, let's display an excerpt
the_title();
the_excerpt();
endwhile;
endif;
?>
这段代码默认将文章显示为摘要,如果要将文章显示为全文的形式,就在自定义域中设定一个字段full,然后给它赋予一个任意值,文章就会在首页以全文形式显示出来。
代码来源: How to: Manually define whether to show full posts or excerpts on your home page
3. 显示心情以及当前收听的歌曲
利用自定义域可以让WordPress用户来设置显示他们在撰写博客时的心情及正在收听的音乐。打开主题文件single.php (或者index.php文件 ), 将下面的代码粘贴到您想要显示心情和音乐的任意一个loop循环中:
$customField = get_post_custom_values("mood");
if (isset($customField[0])) {
echo "Mood: ".$customField[0];
}
在写文章时,在自定义域设定一个字段mood,将它的值设置为你的心情. 这跟在首页的文章摘要旁显示缩略图的方法没有多大区别。
代码来源: How to: Display your current mood on your posts
4. 给文章添加Meta描述
WordPress竟然没有使用Meta描述标签作为预设值。当然,对于seo来说,现在的meta标签已经没有以前那么重要了,然而,尽管如此,它们依然可以提高您的博客在搜索引擎中的排名。
那么,我们又是如何使用自定义字段来创建单篇文章的meta描述的呢?打开文件 header.php。复制下面的代码到 <head> 和 </head>标签内:
<meta name="description" content="
<?php if ( (is_home()) || (is_front_page()) ) {
echo ('Your main description goes here');
} elseif(is_category()) {
echo category_description();
} elseif(is_tag()) {
echo '-tag archive page for this blog' . single_tag_title();
} elseif(is_month()) {
echo 'archive page for this blog' . the_time('F, Y');
} else {
echo get_post_meta($post->ID, "Metadescription", true);
}?>">
为了生成meta描述,这里采用的是WordPress 中广泛采用的条件标签来决定哪些用户会访问这个页面。目录页,标签页,存档页及首页,使用的都是静态 meta 描述语。编辑3,7,9行来定义您自己的页面。这样,在文章中,代码会寻找自定义字段 Metadescription,并将其中的值作为 meta 描述语。
代码来源: Unique meta description and meta keyword tags in your WordPress themes
5. 链接到外部资源
引文:“我如何直接链接到外部来源,而不是创建一篇文章,仅仅是为了告诉读者们怎么去访问其他站点?”。这个问题可以通过自定义字段来解决。
首先要做的是,打开主题文件functions.php,粘贴下面的代码到里面:
function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);
if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey=='url1' || $pkey=='title_url' || $pkey=='url_title') {
$post_val = get_post_custom_values($pkey);
}
}
if (empty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
}
} else {
$link = $perm;
}
echo '<h2><a href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a></h2>';
}
保存之后,再打开index.php文件,找到如下输出部份的标准代码:
<h2><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></a></h2>
替换为如下这一句:
<?php print_post_title() ?>
好了,当你要将文章标题指向博客之外的文章时,只要创建自定义字段url1或title_url或url_title ,再输入外部URL地址作为值就可以了。完成以上操作后,当你发表一篇文章时,系统会查找字段url1或title_url或url_title的值,如果找到,那么就会直接链接到外部站点而不是本博客文章;如果没有找到自定义字段值,就会简单地显示链接本身。
代码来源:How to: Link to an external resource in the post’s title
6.给博客文章添加额外的CSS样式
某些文章有时可能需要加上一些额外的CSS 样式。当然了,你可以选择在WordPress编辑器的html 模式下进行编辑,加入内置CSS样式到文章中。但是,尽管内置样式是有效的,可这并不是最简单的处理方法。如果使用自定义字段的话,我们可以很容易地为某篇文章添加新的CSS类,并自动加载它们到博客的header 中。
方法如下:打开header.php文件 ,然后添加如下的代码到HTML标签<head>和</head>之间:
<?php if (is_single()) {
$css = get_post_meta($post->ID, 'css', true);
if (!empty($css)) { ?>
<style type="text/css">
<?php echo $css; ?>
<style>
<?php }
} ?>
在你撰写文章时自定义一个字段CSS,再为它赋予样式代码作为值即可。
7. 重新定义<title>标签
wordpress默认主题并没有对标签<title>做过优化。许多博主都是通过使用插件All in One SEO Pack来实现<title>的优化的,但是我们仍然可以通过自定义字段来解决此问题。
打开header.php文件,找到<title>标签,并将其替换为下面的代码:
<title>
<?php if (is_home () ) {
bloginfo('name');
} elseif ( is_category() ) {
single_cat_title(); echo ' - ' ; bloginfo('name');
} elseif (is_single() ) {
$customField = get_post_custom_values("title");
if (isset($customField[0])) {
echo $customField[0];
} else {
single_post_title();
}
} elseif (is_page() ) {
bloginfo('name'); echo ': '; single_post_title();
} else {
wp_title('',true);
} ?>
</title>
之后,在你想自定义title标签时,只要创建一个自定义字段title,为它赋值就OK了。
这段代码使用不同模板标签分别为首页, 分页, 目录页及博客文章各生成一个自定义的<title> 。如果当前页面是一篇博客文章,代码会寻找自定义字段title。如果找到了,就会显示自定义字段中的值,反之,显示标准的single_post_title() 生成的标题。
代码来源:How to: Redefine the title tag with a custom field
8. 禁止搜索引擎索引博客文章
如果我们平时写作一些博客文章只是想给特定的读者阅读,而并不希望被搜索引擎抓取,可以通过自定义字段来实现。 首先,获取你不想被搜索引擎索引的文章的ID号。下面我们以ID为17的为例。
打开主题header.php文件,粘贴下面的代码到标签 <head>和</head>之间:
<?php $cf = get_post_meta($post->ID, 'noindex', true);
if (!empty($cf)) {
echo '<meta name="robots" content="noindex"/>';
}
?>
在这个例子中,使用了get_post_meta()的方法来检索自定义字段noindex,如果它的值设置为17,那么将添加noindex(不索引)的标记 。
9. 用自定义函数轻松获取或打印自定义字段值
获得任何自定义字段值的方法其实也很简单。将下面的代码粘贴到主题文件functions.php中(如果你的主题没有这个文件,那么创建一个!)。
function get_custom_field_value($szKey, $bPrint = false) {
global $post;
$szValue = get_post_meta($post->ID, $szKey, true);
if ( $bPrint == false ) return $szValue; else echo $szValue;
}
然后使用下面的代码来调用:
<?php if ( function_exists('get_custom_field_value') ){
get_custom_field_value('featured_image', true);
} ?>
代码分析:首先,我们使用PHP函数 function_exists()来判断是否存在get_custom_field_value,如果存在,我们就可以使用。第一个参数是自定义字段名称 (这里是featured_image),第二个参数是允许您直接输出的值 (true), 或者写成false以作其它更深一层的PHP之用。
代码来源:How to: Easily get the value of a custom field
10.根据需要为博客文章添加Digg按钮
要使你的文章从Digg.com获得更多点击量的最好的方法是将“Digg this”按钮添加到您的每一篇文章中,这样读者就可以协助您成功发布这篇文章。
我们还是利用自定义字段来完成,只要按照以下步骤即可实现:
1). 打开主题中的single.php文件,将下面的代码粘贴到你想要显示“Digg this”按钮的地方:
<?php $cf = get_post_meta($post->ID, 'digg', true);
if (!emptyempty($cf)) {
echo 'http://digg.com/tools/diggthis.js" type="text/javascript">'} ?>
2). 保存了文件single.php后,你就可以创建自定义字段digg了,然后给它赋予一个任意的值。如果赋值,按钮就会显示在文章中了。代码运行后如果找到自定义字段digg,则显示“Digg this” 按钮。Digg本身提供了用户用以显示“Digg this” 按钮的JavaScript。
11.通过自定义字段显示文章缩略图
首先用图像处理工具Photoshop或Gimp制作一张要默认显示的image,格式大小以本例中的200×200像素为例,然后将其命名为default.gif,并上传到主题的images目录下。
打开index.php文件,在里面你想要显示缩略图的地方粘贴以下代码:
<?php $postimageurl = get_post_meta($post->ID, 'post-img', true);
if ($postimageurl) {
?>
<a href="<?php the_permalink(); ?>" rel="bookmark"><img src="<?php echo $postimageurl; ?>" alt="Post Pic" width="200" height="200" /></a>
<?php } else { ?>
<a href="<?php the_permalink(); ?>" rel="bookmark"><img src="<?php bloginfo('template_url'); ?>/images/wprecipes.gif" alt="Screenshot" width="200" height="200" /></a>
<?php } ?>
保存文件. 之后,在你撰写文章时自定义一个字段为post-img,并为它赋予一个图片url作为值,文章发表时就会显示缩略图了。
12.给某篇文章添加js或者CSS
如果我们要给博客文章单独加载JS、CSS,那么将下面这段代码复制到你主题根目录下的functions.php 文件中:
function head_JS_CSS(){if (is_single() || is_page()) {global $post;$head_JS_CSS = get_post_meta($post->ID, 'head_JS_CSS', true);echo $head_JS_CSS;}}add_action("wp_head","head_JS_CSS");
写作文章时,在WordPress自定义区域处创建自定义字段为:head_JS_CSS,为它赋予你想要的Javascript或CSS代码作为值即可。
13.给加密的文章添加密码提示信息
将以下代码粘贴到主题的functions.php文件中:
function password_hint( $c ){global $post, $user_ID, $user_identity;if ( empty($post->post_password) )return $c;if ( isset($_COOKIE['wp-postpass_'.COOKIEHASH]) && stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) == $post->post_password )return $c;//替换if($hint = get_post_meta($post->ID, 'password_hint', true)){$url = get_option('siteurl').'/wp-pass.php';if($hint)$hint = '密码提示:'.$hint;else$hint = "请输入您的密码";if($user_ID)$hint .= sprintf('欢迎进入,您的密码是:', $user_identity, $post->post_password);$out = <<<END<form method="post" action="$url"><p>这是一篇受密码保护的文章,请输入密码后再继续阅读:</p><div><label>$hint<br/><input type="password" name="post_password"/></label><input type="submit" value="Submit" name="Submit"/></div></form>END;return $out;}else{return $c;}}add_filter('the_content', 'password_hint');
写作文章时自定义一个字段为password_hint,赋予其一个任意值作为密码提示信息,如:某某人的生日是几号?
14.一个自定义字段赋予多个值的使用方法
将下面的代码复制到主题目录的functions.php文件中:
function article_source() {global $post;$article_source = get_post_meta($post->ID, article_source,false);if($article_source) {foreach ($article_source as $article_sources){$fullValue = explode ("|", $article_sources);$name = $fullValue[0];$address = $fullValue[1];}echo '译文来源:<a href="'.$address.'" target="_blank">'.$name.'</a>,';}else {echo '本站原创';}}
在撰写文章时,如果是译文,则为其添加一个名称为 article_source 的自定义字域,并赋予其两个值,格式为“网站名|文章地址”。原创的文章则不添加这个自定义域。