wordpress高级应用,的确感觉wordpress是越来越强大了,利用它不仅仅可以做出博客,而且更可以成为内容管理系统。在本文中,收藏一些Wordpress 自定义域的技巧和应用。
1 2 3 4 5 6 7 8 9 10 | <?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 } ?> |
1 2 3 4 5 6 | <?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的自定义域名称是 head_JS_CSS。那么你首先需要把下面这段代码复制到你主题根目录下的 functions.php 文件中:
1 2 3 4 5 6 7 8 | 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 文件中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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的生日是几号?
1 2 3 4 | <span style="font-family: monospace;"><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></span> |
然后在你添加日志的时候,在WordPress自定义域区域,创建一个新的名称为:”image_thumb”自定义域,在“值”输入你要为本篇日志上传 的图片的 URL。
我们只需要找到主题文件夹的根目录下的functions.php文件,添加以下代码即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <span style="font-family: monospace;">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');</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <span style="font-family: monospace;">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 '本文为本站原创,'; } }</span> |
将上面的代码复制到 WordPress 主题目录的 functions.php 中,然后在合适的地方调用这个函数即可。对于翻译的文章,需要给文章添加一个名称为 article_source 的自定义域,里面存放两个值,方式为“网站名|文章地址”。原创的文章则不添加这个自定义域。
下面简单解释一下这段代码。get_post_meta() 这个函数会调用指定自定义域中的值,通过其参数我们可以控制是作为一个字符串 调用还是作为一个数组调用以便接下来的处理,详细的用法请参见函数的说明。我这里使用 false 参数将函数设定为作为数组提取。接下来使用 foreach 语句遍历数组,讲结果存放到一个新的变量中,再用 explode 语句以“|” 符号为标记对其进行切割。最后就是把切割开的值分别存放到两个新的变量中以供提取使用。
有了这个例子,你就可以很方便的修改上面给出的代码来实现自己需要的功能了。


后退
Void
Life
Earth
Wind « Default
Water
Fire
Light 
呵呵 很不错 收下,wp学不完
我也是转载的,互相学习才能进步:)