扫一扫加微信

WordPress“自定义域”使用实例

wordpress高级应用,的确感觉wordpress是越来越强大了,利用它不仅仅可以做出博客,而且更可以成为内容管理系统。在本文中,收藏一些WordPress 自定义域的技巧和应用。

优化wordpress SEO

<?php if ( is_home() ) { ?>
<meta name="description" content="。。。" />
<meta name="keywords" content="。。。" />
<?php } ?>
<?php if ( is_single() ) { ?>
<meta name="description" content="<?php $key="description"; echo get_post_meta($post->ID, $key, true); ?>" />
<?php } ?>
<?php if ( is_category() ) { ?>
<meta name="description" content="<?php echo category_description(); ?>" />
<?php } ?>

单篇日志和PAGE页面的TITlE优化

<?php if (is_home() ) { ?>首页标题<?php }?>
<?php if( get_post_meta($post->ID, "title_single", true) ): ?>
<?php echo get_post_meta($post->ID, "title_single", true); ?>|站点名称
<?php else: ?>
<?php wp_title(''); ?>|站点名称
<?php endif; ?>

给某一日记添加js或者CSS

假设我们给日志单独加载JS、CSS的自定义域名称是 head_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 即可。

给加密的日志添加密码提示信息

假设我们给日志添加密码提示的自定义域名称是password_hint。那么你首先需要把下面这段代码复制到你主题根目录下的 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');

然后在你添加日志的时候,在WordPress自定义域区域,创建一个新的名称为:”password_hint”的自定义域,在“值”输入你密码提示: 如xx的生日是几号?

显示日志缩略图

<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<img src="<?php $values = get_post_custom_values("image_thumb"); echo $values[0]; ?>" alt="<?php the_title(); ?>" />
</a>

然后在你添加日志的时候,在WordPress自定义域区域,创建一个新的名称为:”image_thumb”自定义域,在“值”输入你要为本篇日志上传 的图片的 URL。

WordPress添加自定义域提示

我们只需要找到主题文件夹的根目录下的functions.php文件,添加以下代码即可:

function custom_fields_tip(){
$h3 = "欢迎使用自定义域";
$html=<<<END
<div>
<h3>$h3</h3>
<div>
<p>如果你正在发布或编辑一篇受密码保护的文章,建议您添加一个名称为'password_hint'的自定义域.用来提示访客.<br/>使用'head_JS_CSS'可以添加css、js到头部.<br/>使用'image_thumb'可以给文章添加缩略图</p>
</div>
</div>
END;
echo $html;
}
add_action('submitpost_box', 'custom_fields_tip');
add_action('submitpage_box', 'custom_fields_tip');

一个自定义字段中存放多个值

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 '本文为本站原创,';
}
}

将上面的代码复制到 WordPress 主题目录的 functions.php 中,然后在合适的地方调用这个函数即可。对于翻译的文章,需要给文章添加一个名称为 article_source 的自定义域,里面存放两个值,方式为“网站名|文章地址”。原创的文章则不添加这个自定义域。

下面简单解释一下这段代码。get_post_meta() 这个函数会调用指定自定义域中的值,通过其参数我们可以控制是作为一个字符串 调用还是作为一个数组调用以便接下来的处理,详细的用法请参见函数的说明。我这里使用 false 参数将函数设定为作为数组提取。接下来使用 foreach 语句遍历数组,讲结果存放到一个新的变量中,再用 explode 语句以“|” 符号为标记对其进行切割。最后就是把切割开的值分别存放到两个新的变量中以供提取使用。

有了这个例子,你就可以很方便的修改上面给出的代码来实现自己需要的功能了。

58 评论

9+7=

  1. 鸭宝宝

    来你博客逛逛!

    回复
  2. 驻美解放军

    这样也行?高!

    回复
  3. 软件酷的Tmd

    来访了,支持下博主

    回复
  4. 学会感恩

    很好的文章,非常喜欢,呵呵,不错,这种好文章不多,博主辛苦了。

    回复
  5. 工业加湿器

    博主牛叉,文章牛叉!

    回复
  6. 南京seo

    或许在现在学习seo最好的方式 ,就是博客上交流了

    回复
  7. 滨州装修网

    很喜欢楼主的模版风格。请问是自己做的吗?

    回复
  8. 诸城招聘网

    初次来访,觉得博主的博客很不一样,会常来的。

    回复