我这几天都在做WordPress主题了
我感觉自己似乎一直在浪费时间,一直都是在给别人做WP主题,当然也是为了赚点钱。每次一开电脑就要打开Netbeans,还有一个为了测试主题而安装的Windows虚拟机。。。
自己想学的都还没有学到,很多的事情都荒废了。不过也学到了点东西,至少更会做网页了,还会了点PHP,算是意外的收获。为了更好的写代码,还学习了一下使用Mercurial,之前我是从来没用过版本控制软件的,因为之前就没写过太多代码。
没有什么说的了,贴一点做主题时用到的简单的代码,说不定哪天有哪位与ABitNo一样菜的朋友会用到。不过想想也不知道哪些代码比较有用,就把我在sidebar里用的几个方法帖一下。
获取WordPress中评论最多的文章
function wp_get_most_commented_posts($limitclause="") { global $wpdb; $q = "SELECT ID, post_title, post_date, COUNT($wpdb->comments.comment_post_ID) AS 'comment_count' FROM $wpdb->posts, $wpdb->comments WHERE comment_approved = '1' AND post_status = 'publish' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID GROUP BY $wpdb->comments.comment_post_ID ORDER BY comment_count DESC $limitclause"; return $wpdb->get_results($q); }
获取并输出WordPress的相关文章列表,如果没有相关文章就显示评论最多的文章,这个用到了上面获取评论最多文章的方法
function wp_get_related_posts() { global $wpdb, $post,$table_prefix; $now = current_time('mysql', 1); $tags = wp_get_post_tags($post->ID); $taglist = "'" . $tags[0]->term_id. "'"; $tagcount = count($tags); if ($tagcount > 1) { for ($i = 1; $i <= $tagcount; $i++) { $taglist = $taglist . ", '" . $tags[$i]->term_id . "'"; } } $q = "SELECT p.ID, p.post_title, p.post_date, p.comment_count, count(t_r.object_id) as cnt FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships t_r, $wpdb->posts p WHERE t_t.taxonomy ='post_tag' AND t_t.term_taxonomy_id = t_r.term_taxonomy_id AND t_r.object_id = p.ID AND (t_t.term_id IN ($taglist)) AND p.ID != $post->ID AND p.post_status = 'publish' AND p.post_date_gmt < '$now' GROUP BY t_r.object_id ORDER BY cnt DESC, p.post_date_gmt DESC $limitclause;"; $related_posts = $wpdb->get_results($q); $output = ""; if (!$related_posts) { $related_posts = wp_get_most_commented_posts("LIMIT 10"); foreach ($related_posts as $related_post ) { $output .= '<li>'; $output .= '<a href="'. get_permalink($related_post->ID). '" title="'.wptexturize($related_post->post_title). '">'.wptexturize($related_post->post_title).''; $output .= '</a></li>'; } $output = '<ul class="related_post">' . $output . '</ul>'; $output = "<div class='most-comment'><h2>Most Commented</h2></div>". $output; return $output; } foreach ($related_posts as $related_post ) { $output .= '<li>'; $output .= '<a href="'.get_permalink($related_post->ID). '" title="'.wptexturize($related_post->post_title).'">'. wptexturize($related_post->post_title).''; $output .= '</a></li>'; } $output = '<ul class="related_post">' . $output . '</ul>'; $output = "<div class='related'><h2>Related Posts</h2></div>". $output; return $output; } function wp_related_posts() { $output = wp_get_related_posts() ; echo $output; }
<获取并输出WordPress中的最新留言列表,并且不包含ABitNo自己写的评论,每条留言最多只显示30个字,后面加个省略号。其中的一个方法utf8Substr是用来截取utf字符串的。
function wp_list_newest_comments($limitclause="LIMIT 10") { global $wpdb; $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url, comment_content FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_author!='ABitNo' AND comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC $limitclause"; $comments = $wpdb->get_results($sql); $output = ""; foreach ($comments as $comment) { $comment_cont=utf8Substr(strip_tags($comment->comment_content), 0, 30)."..."; $output .= "<li><span>".strip_tags($comment->comment_author) ."</span>: <a href=\"" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "\" title=\"on " . $comment->post_title . "\">" .$comment_cont ."</a></li>"; } echo $output; } function utf8Substr($str, $from, $len) { return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'. $from. '}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'. $len. '}).*#s', '$1',$str); }
把这些代码放到WordPress主题的functions.php文件中就可以直接调用了。上面前两段代码改自WP的一个插件WordPress 2.3 Related Posts Plugin,获取最新留言的代码也取自google并做了些修改,已经不知道最早的作者是谁了。
本文基于 署名-非商业性使用-禁止演绎 2.5 中国大陆 发布
24 COMMENTS >>LEAVE<<
-
看见你了线了,觉得应该会有更新吧,真的有,哈哈,沙发~~
做主题很快就会腻味了,不过开发个插件什么的就不同了,很有乐趣!你试试 -
来支持下。主题很简洁漂亮!
不同意kangzj 哦~主题变化无穷,侧重审美;
开发插件就靠IDEA和编写了,所以各有侧重,都有乐趣! -
@Panda
谢谢。。。
同意你的说法。。。哈哈。。。 -
最近我也有点想弄自己的主题
-
我用inove是因为这个清爽不用自己改了。。。。
-
@messiahxu
那还是我的清爽,自恋。。。
-
偶悄悄的期待着!
-
@ABitNo
这两天看了点CSS..有点想动手了..
我想做个双栏主题..
左边生活..右边技术.. -
最新评论你也是用截取长度的,哈哈,我的也是朋友帮忙写的。
不过能用CSS控制长度就更完美了,我新建的页面——下载专区,就是这样,我借鉴自一款CMS主题的。
那企鹅怎么没颜色呢,要是彩色的把肚皮挖空就好了。 -
@xifs
双栏主题也不错,看过几个双栏的,不过是两个人写的,左边一个人,右边一个人
-
@LAONB
我这是从官方网站上找的logo,也有那种黄色的。。。不过感觉这个黑白的配上我这个主题还是蛮有感觉的。。。
-
学习为了更好的赚钱,手头有点钱之后收手学习吧。。。
-
@young001
是的。。。这样每天都在做这些东西感觉很累,似乎都是負担了。。。不过每次我还没做完一个,就会有下一个人来找我做。。。
-
色彩搭配不错
-
@ABitNo
生意好好啊,这些人怎么都知道你在找活?没干过这行,不懂啊
-
@ABitNo
哈哈,那帖子我也看了,好好努力吧
-
辛苦了.
-
为什么不能下载呢
-
@eben
下载什么呢?这个主题吗?
-
@ABitNo
嗯 这个博客的主题 我也挺喜欢的 能够居右 然后左边放广告就更好了
-
@eben
真不容易啊,有人喜欢我这个主题。。。
我发到你的email里吧,就发到你给我评论的这个email里