自动在正文内容后添加内容
很多时候,你都需要在文章内容后面添加一些信息,例如订阅,文章分享,收藏和Creative Commons协议声明等。一般情况下,你可以直接编辑主题的single.php文件添加代码来达到目的。但在制作主题的时候,每个用户的需求都不同,而且你也不可能在文章下方添加太多的内容。因此让用户能自定义自己需要的内容是最好的方案。
很多时候,你都需要在文章内容后面添加一些信息,例如订阅,文章分享,收藏和Creative Commons协议声明等。一般情况下,你可以直接编辑主题的single.php文件添加代码来达到目的。但在制作主题的时候,每个用户的需求都不同,而且你也不可能在文章下方添加太多的内容。因此让用户能自定义自己需要的内容是最好的方案。
本文参考了Xiaorsz的使用 jQuery 实现 wordpress 的 Ajax 留言。
前几天,为了提高评论体验,为了减轻服务器负担,我为主题添加了AJAX提交评论功能,现在分享一下方法。
首先需要在主题的function.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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | < ?php function fail($s) { header('HTTP/1.0 500 Internal Server Error'); echo $s; exit; } function ajax_comment(){ if($_POST['action'] == 'ajax_comment') { global $wpdb, $db_check; // Check DB if(!$wpdb->dbh) { echo('Our database has issues. Try again later.'); die(); } nocache_headers(); $comment_post_ID = (int) $_POST['comment_post_ID']; $status = $wpdb->get_row("SELECT post_status, comment_status FROM $wpdb->posts WHERE ID = '$comment_post_ID'"); if ( empty($status->comment_status) ) { do_action('comment_id_not_found', $comment_post_ID); fail('The post you are trying to comment on does not currently exist in the database.'); } elseif ( 'closed' == $status->comment_status ) { do_action('comment_closed', $comment_post_ID); fail('Sorry, comments are closed for this item.'); } elseif ( in_array($status->post_status, array('draft', 'pending') ) ) { do_action('comment_on_draft', $comment_post_ID); fail('The post you are trying to comment on has not been published.'); } $comment_author = trim(strip_tags($_POST['author'])); $comment_author_email = trim($_POST['email']); $comment_author_url = trim($_POST['url']); $comment_content = trim($_POST['comment']); // If the user is logged in $user = wp_get_current_user(); if ( $user->ID ) { $comment_author = $wpdb->escape($user->display_name); $comment_author_email = $wpdb->escape($user->user_email); $comment_author_url = $wpdb->escape($user->user_url); if ( current_user_can('unfiltered_html') ) { if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment'] ) { kses_remove_filters(); // start with a clean slate kses_init_filters(); // set up the filters } } } else { if ( get_option('comment_registration') ) fail('Sorry, you must be logged in to post a comment.'); } $comment_type = ''; if ( get_option('require_name_email') && !$user->ID ) { if ( 6> strlen($comment_author_email) || '' == $comment_author ) fail('Sorry: please fill the required fields (name, email).'); elseif ( !is_email($comment_author_email)) fail('Sorry: please enter a valid email address.'); } if ( '' == $comment_content ) fail('Sorry: please type a comment.'); // Simple duplicate check $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' "; if ( $comment_author_email ) $dupe .= "OR comment_author_email = '$comment_author_email' "; $dupe .= ") AND comment_content = '$comment_content' LIMIT 1"; if ( $wpdb->get_var($dupe) ) { fail('Duplicate comment detected; it looks as though you\'ve already said that!'); } $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_ID'); if( !$user->ID ){ $result_set = $wpdb->get_results("SELECT display_name, user_email FROM $wpdb->users WHERE display_name = '" . $comment_author . "' OR user_email = '" . $comment_author_email . "'"); if ($result_set) { if ($result_set[0]->display_name == $comment_author){ fail( __('Error: you are not allowed to use the nickname that you entered.if you are the administrator you hava to login to comment.','philna2') ); } else { fail( __('Error: you are not allowed to use the email that you entered.if you are the administrator you hava to login to comment.','philna2') ); } } } $comment_id = wp_new_comment( $commentdata ); $comment = get_comment($comment_id); if( !$user->ID ){ setcookie('comment_author_' . COOKIEHASH, $comment->comment_author, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); setcookie('comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); setcookie('comment_author_url_' . COOKIEHASH, clean_url($comment->comment_author_url), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); } @header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset')); ?> //这里需要粘贴你的评论框架代码,不过相关的调用代码有所变化: //评论ID:$comment->comment_ID //评论者名字:$comment->comment_author //判断评论者是否填写了网站地址:$comment->get_comment_author_url //评论者URL:$comment->comment_author_url //评论时间:mysql2date(__('F jS, Y'),$comment->comment_date) //评论者e-mail:$comment->comment_author_email //评论内容$comment->comment_content < ?php die(); } } add_action('init', 'ajax_comment'); //添加AJAX评论钩子 ?> |

Blues主题由Bolo设计,是一个配色清新,布局简洁的WordPress主题。使用了jQuery框架,能简单地通过jQuery插件进行功能扩展和界面美化。此主题通过W3C的XTML 1.1 和 CSS3 验证,并在IE 6/7/8,Firefox 3.5,Chrome 2,Safari 4,Opera 10 上测试,显示效果一致。主题带有后台选项,可以方便地发布网站公告和广告等。更多功能将在后续版本中加入。
经过一个星期的努力,终于赶制出现在这个主题.深有感触,发泄一下.
这个主题是以Default为基础的,但结构已经被我改写了,同时我还借鉴了其他主题一些比较好的设计,如Lightword的外飘式日期和inove的customs comments.但总算是一个令自己感到满意的主题,因为折叠的导航栏和外飘的sidebar是我自己设计的,还有一直喜欢的大大的搜索框.这次我大胆地使用了几个纯色块来装点主题,在选颜色的时候我已经认真地考虑了色彩的明度问题,因此在我这里看来,应该不会有刺眼的问题(我曾经做过的PJ-Blog主题曾经被人批评背景色太刺眼).
今天一睡醒,检查邮箱发现WordPress给我发来了主题审核结果,全文如下:
While reviewing Inpad – 1.1 we found the following:
- When viewing in Firefox 3.5.2, the sidebar always appears below the posts.
- It should be possible to replace default widgets from the Appearance > Widgets page (i.e. it would be nice if you could add a bottomsidebar widget area).
- Please specify a GPL-compatible license in style.css.If you haven’t already please test your theme with the sample data in the WordPress export file available at http://codex.wordpress.org/Theme_Development_Checklist#.22Theme_Unit_Tests.22.
To update your theme increase the theme version in style.css and upload your theme again.
You can upload updates to your theme at any time, they will automatically show up in our review queue.
Thanks.
有点失望,接下来几天会继续完善主题,添加一些新功能,希望下次能通过审核。
经过重重困难和几乎没谱的制作时间,本人第一个WordPress主题:Inpad,终于发布了。
Inpad是一个简洁的灰色调主题。本来想设计成一个有时尚味道的日记本型主题,但在配色时不小心陷进了灰色调里,显得很不时尚。本来我想给主题改名,但后来仔细考虑后还是不改了,以后弄多几个配色好了。先看主题全身像吧!点击有大图。
这个主题利用了jQuery实现一些比较炫的效果,例如comment-tip,页内平滑滚动等等。好奇的同学可以玩一下。另外在我打理鼓吹其好处的大搜索框下面,还专门设计了一个Twitter链接,以满足广大Twitter控们。在Twitter上follow了我的童鞋可以留意一下,之前我说过做出了一个非常帅的评论样式,我已经做进Inpad里了。
暂时就说这么多了,主题已经提交到官方目录,正在漫长的审核中。为了履行在这个星期发布的诺言,我先泄露一下,下载地址是:
Lifhtword是Andrei Luca设计的一款简洁两栏主题。由于作者出于美观考虑,把主题中标题的字符都用图片代替,可是这样就不能显示中文了,效果?请点击右图。
产生这种现象的原因是主题加载了mp.font.js和extra_mp.font.js这两个脚本,由于主题提供的字库里并没有中文字体,所以中文就无法显示了。要想让它支持中文,最好的办法就是不加载这两个脚本了。我们应该怎么修改呢?
打开主题的funtion.php,在最底下的地方找到:

现在inove主题的流行程度已经不用再说了,十个wp博客起码4个是inove。这款主题的修改者也很多,但大多还是摆脱不了mg12的阴影(包括我)。不少人想把inove改成三栏,但我还没看见有哪个人改得成功。mg12已经在博客上说过不会发布三栏版的inove,留意一下它以往的主题,根本不见三栏的踪影,因此可以确定mg12热衷于两栏。
其实原版的inove挺整洁的,但缺点也十分明显:当分类或页面比较多的时候,显示的效果将非常糟糕。
现在流行整合和自定义,wp主题也不例外。越来越多的主题作者开始在主题里整合一些插件,如philna2主题,已经整合了quick comments插件,能实现ajax提交评论。也有一些主题的自定义功能相当强,如inove主题,和上面提到的philna2。
但多功能就意味着体积庞大,这些主题如果不优化一下的话,常常会造成页面打开非常缓慢。我们必须对它进行精简,才能把主题化为自己所用。
首先是去掉不需要的自定义功能,请看摘抄字inove的一段代码。
<ul id="menus"> <li class="<?php echo($home_menu); ?>"><a class="home" title="<?php _e('Home', 'inove'); ?>" href="< ?php echo get_settings('home'); ?>/">< ?php _e('Home', 'inove'); ?></a></li> < ?php if($options['menu_type'] == 'categories') { wp_list_categories('depth=2&title_li=0&orderby=name&show_count=0'); } else { wp_list_pages('depth=2&title_li=0&sort_column=menu_order'); } ?> </ul>
inove原版的订阅菜单有两个问题:一是没注意页面的层次,导致菜单会被google ads单元遮住,影响使用;二是双列结构,增减订阅按钮必须每次两个两个地来,否则不好看。
我比较喜欢mg12的blocks系列主题的订阅菜单,很简洁,效果很不错。因此今天把它移植到inove的导航栏上了。本来想写个教程,但由于我是在已有修改的基础上改的,没在原版主题上测试过,发出来意义也不大。因此我只提供我的思路和一些提示,喜欢的话,就自己diy一下吧,有问题的话,可以问我。在时间和能力许可的情况下,我会尽力帮你解决的。
首先说明一下,实现这个效果是很容易的,我参考了mg12的《主题技巧: 滚动导航菜单》一文,js脚本我完全没改过,所以你想做我这种菜单的话,应该好好看看mg12的demo。css样式可以参考我的,自己到源代码里看。
对于feed地址,可以用php先设置一个值$feed = ‘你的feed地址’,然后在订阅器链接上调用,这样以后修改起来方便点。
最后说说页面层次的问题,要用到z-index这个css属性。
Recent Comments