[分享]我实践的一些WordPress高级应用 »

  • 12
  • 9月

[分享]我实践的一些WordPress高级应用

Lava 发布于 15:12:19  |  评论 (0)  |  阅读:155 次  

WordPress是开源的PHP博客模板,其可以免费使用的插件成千上万,如果你想要的功能,没能找到合适的插件,其实简单的修改代码也能给你很多惊喜。

1、.htaccess文件修改,让博客更多彩:
合理的运用.htaccess的URL重写功能可以实现WORDPRESS不同分类页面的不同显示,例如本博客首页第一条显示全文,其他文章显示描述内容,全部文章显示文章标题和标签、分类,等…
这是如何实现的呢?我们先看.htaccess文件

RewriteEngine On
#404
ErrorDocument 404 /error_404.html
AddDefaultCharset UTF-8
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule . /index.php [L]
#/2008-09-03/92.html
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)\.html$ /index.php?action=post [L,NS,QSA]
#category/all
RewriteRule ^category/all$ /index.php?action=all [L,NS,QSA]
RewriteRule ^category/all/page/([a-z0-9\-]+)$ /index.php?action=all [L,NS,QSA]
#map
RewriteRule ^map\.html$ /index.php [L]
#/category/uncategorized
RewriteRule ^category/([\w\-\.]+)$ /index.php [L]
#/category/uncategorized/think
RewriteRule ^category/([\w\-\.]+)/([\w\-\.]+)$ /index.php [L]
#/2008/09
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ /index.php [L]
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/page/([a-z0-9\-]+)$ /index.php [L]
#/2008/09/03
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)$ /index.php [L]
#tag/
RewriteRule ^tag/(.*)$ /index.php [L]
#feed
RewriteRule ^feed$ /index.php [L]
#comments/feed
RewriteRule ^comments/feed$ /index.php [L]
#about
RewriteRule ^about$ /index.php [L]


 以上这段RewriteRule ^category/all$ /index.php?action=all [L,NS,QSA]
及如果ur地址是http://www.onecho.com/category/all的话则跳到/index.php?action=all 页面(WORDPRESS几乎所有文章的页面都是index.php处理的),这样我们就得到了action=all的参数,然后修改index.php文件:

<?
if ($action == "all") {
 $all_index++;
 $color_on = ($all_index % 2) ? "color_off" : "color_on";
?>
<div class="post_all <?=$color_on?>" id="post-<?php the_ID(); ?>">
<h3><a href="<?php the_permalink() ?>" rel="bookmark" ><?php the_title(); ?></a></h3>
<div class="meta"><?php the_author() ?> 发布于 <?php the_date() ?> <?php the_time() ?> <?php edit_post_link(__('Edit This')); ?> <?php comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)')); ?>&nbsp;&nbsp;阅读:<?php the_views(); ?></div>
<p>
 <?php _e("分&nbsp;&nbsp;类:"); ?>
 <?php the_category(',') ?><br />
 <?php the_tags(__('标&nbsp;&nbsp;签:&nbsp;'), ', ', ''); ?>
</p>
</div>
<?php
}

即得到action参数的值,如果等于”all“,我们只显示文章标题和分类、标签等信息。其他的话,我们在现实我们想要的页面。比如只在文章页显示前后页,我们可以这么做:
#/2008-09-03/92.html
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)\.html$ /index.php?action=post [L,NS,QSA]
这时参数action的值为post,我们在php里面这样判断:

$action = "";
$action = $_GET["action"];
<?php
if ($action == "post") {
?>
<div class="next_link">
 <table width="100%" bgcolor="#F8F7EF">
  <tr>
   <td width="10%" align="center">前一篇: </td>
   <td><div style="overflow:hidden;"><?=!empty($previous_mb) ? $previous_mb : "没有了" ?></div></td>
  </tr>
  <tr>
   <td width="15%" align="center">后一篇: </td>
   <td><div style="overflow:hidden;"><?=!empty($next_mb) ? $next_mb : "没有了" ?></div></td>
  </tr>
 </table>
</div>
<?php
 echo "<ul class=\"post_relate\"><li id=\"related_posts\" class=\"widget\">";
 wp_related_posts();
 echo "</li></ul>";
}

 怎么样,只要你想在不同的页面不同显示,就都可以用修改url重写和php文件实现,这里注意对.htaccess和php不熟悉的朋友不要随便修改。

2、自定义字段的应用:

WordPress拥有一个强大的功能custom fields 可以让你灵活的增加很多额外的信息到你的文章里面. Custom fields由一个名称和一个值组成. KEY是自定义字段的名称和你想要给这个名称指定的值.你可以显示此自定义信息到你的日志,页面或是侧边栏或是网站中的任何地方. WordPress能够记住你使用过的自定义字段,当你下次再使用的时候,只需要从下拉菜单中进行选择即可。
  get_post_meta($post_id, $key, $single);<?php echo get_post_meta($post->ID, ‘key name’,true) ?>
$post->ID 用来获取日志 ID, $key是一个String类,包含你想使用的meta值名称, 而$single则是用来判断真还是似,也就是True or False。 如果设置为True,该功能将返回一个单一的结果作为字符串。 如果设为False,或者不设置 , 则返回自定义字段的一个数组.

先说这么多,以后我们继续(还会介绍页面样式的美化)……

原创文章如转载,请注明:转载自等待喝彩 [ http://www.onecho.com ]
本文链接地址:http://www.onecho.com/2008-09-12/295.html

分  类:PHP
标  签:,
0 Responses to:  “[分享]我实践的一些WordPress高级应用”

No comments yet.

Leave a comment

  • :em41:
  • :em32:
  • :em56:
  • :em72:
  • :em24:
  • :em38:
  • :em57:
  • :em26:
  • :em25:
  • :em21:
  • :em31:
  • :em47:
  • :em55:
  • :em42:
  • :em30:
  • :em59:
  • :em69:
  • :em46:
  • :em58:
  • :em44:
  • :em29:
  • :em27:
  • :em68:
  • :em23:
  • :em65:
  • :em36:
  • :em48:
  • :em51:
  • :em62:
  • :em22:
  • :em37:
  • :em20:
  • :em19:
  • :em50:
  • :em28:
  • :em35:
  • :em33:
  • :em39:
  • :em43:
  • :em54:
  • :em66:
  • :em67:
  • :em34:
  • :em70:
  • :em64:
  • :em53:
  • :em63:
  • :em49:
  • :em60:
  • :em40:
  • :em71:
  • :em52:
  • :em61:
  • :em45:

Copyright © 2008  OnEcho.com  皖ICP备08102536号  E-mail:aurare[at]gmail.com  Powered by WrodPress