显示各个分类下的最新文章
Published on
July 2, 2007
@ 2:46 pm
fvcity 在我以前写的一篇给“Wordpress 中文工具箱”新增了一个函数一文中留言,他需要一个只显示某个文类下的最新文章的功能。我当时想这种功能一定有某个插件实现,后来转念一想,自己把中文工具箱的函数改一改应该也不难解决。
于是就有了这个函数。
功能:
只显示某个分类下的最新文章。
函数名:
get_recent_catposts()
用法:
<?php get_recent_catposts($no_posts = 5, $catid = 1, $before = ‘<li>’, $after = ‘</li>’, $show_pass_post = false, $skip_posts = 0); ?>
$no_posts:显示文章数,缺省为5条;
$catid: 分类 id,缺省为 1;
$before:每条记录前显示的文字,缺省<li>
$after:每条记录后显示的文字,缺省</li>
$show_pass_post:是(true)/否(false)显示保护了的文章,缺省否(false)
$skip_posts:跳过多少篇文章,缺省为0;
这个功能好像暂时对我用处不大,需要的朋友可以下载去玩玩。我在本机上测试似乎没有问题,如果你发现问题麻烦告诉我一声我好修改。函数直接加在中文工具箱里,下载后覆盖原文件。
下载:mulberrykit.txt (下载后把后缀改成 .php)
20 Comment(s)
fvcity
July 2nd, 2007 at 3:15 pm
谢谢Michael,真的是太谢谢了,嘻嘻
fvcity
July 2nd, 2007 at 3:58 pm
我在最新的2.3测试版本使用出现这样的提示:
WordPress database error: [Table 'dao.wp_post2cat' doesn't exist]
SELECT ID, post_title, post_date, post_content, post_id, category_id FROM wp_posts, wp_post2cat WHERE wp_posts.post_status = ‘publish’ AND wp_posts.post_type=’post’ AND wp_post2cat.category_id = 1 AND wp_posts.ID = wp_post2cat.post_id AND post_password =” ORDER BY post_date DESC LIMIT 0, 5
Michael (Blog Owner)
July 2nd, 2007 at 4:11 pm
我没有 2.3 不好说,不过看起来 2.3 在数据库结构上又作了调整。
Michael (Blog Owner)
July 2nd, 2007 at 4:27 pm
@fvcity: 我在 2.3 上测试了一下,没有发现你说的问题。我觉得你可以
1. 登入到后台,wordpress 会自动帮你转换数据库。
2. 如果不行,升级到最新的 version 再试试。
fvcity
July 2nd, 2007 at 4:37 pm
我是全新安装的,因为数据结构变化了
我加你QQ好不好?
fvcity
July 2nd, 2007 at 4:37 pm
我的QQ是79894390,谢谢我很急,急用,谢谢
fvcity
July 3rd, 2007 at 2:19 am
Michael,麻烦人再改我一下,可能是表改变了的原因!
fvcity
July 3rd, 2007 at 8:18 am
改成了这样还是不行
function get_recent_catposts($no_posts = 5, $catid = 1, $before = ”, $after = ”, $show_pass_post = false, $skip_posts = 0) {
global $wpdb;
$request = “SELECT ID, post_title, post_date, post_content, $wpdb->terms.term_id FROM $wpdb->term_taxonomy, $wpdb->posts, $wpdb->terms WHERE $wpdb->posts.post_status = ‘publish’ AND $wpdb->posts.post_type=’post’ AND $wpdb->terms.term_id = $catid AND $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id “;
if(!$show_pass_post) { $request .= “AND post_password =” “; }
$request .= “ORDER BY post_date DESC LIMIT $skip_posts, $no_posts”;
$posts = $wpdb->get_results($request);
$output = ”;
foreach ($posts as $post) {
$post_title = stripslashes($post->post_title);
// $post_date = mysql2date(’j.m.Y’, $post->post_date);
$permalink = get_permalink($post->ID);
$output .= $before . ‘‘ . $post_title . ‘‘. $after;
}
echo $output;
}
Michael (Blog Owner)
July 3rd, 2007 at 9:51 am
先别做小白鼠了,等 2.3 beta 出来再看看吧。
偶爱偶妻
July 4th, 2007 at 4:32 am
谢谢,等2.3.呵呵!不麻烦您了!
ryana
September 16th, 2007 at 4:14 pm
完全不显示。。我放到sidebar以后,不知道为什么,我的是wp2.2.2,另外,$no_posts = 5, $catid = 1这段话的后面的$用成了全角。。
Michael (Blog Owner)
September 17th, 2007 at 12:18 am
@ryana, 全角半角的问题要注意一下,例子里的单引号也是全角的。
酷辣客
October 11th, 2007 at 2:42 am
如果博主把这个函数和前面那个修改的最新留言函数一起集成到中文工具箱就好了,呵呵,我是个代码盲。
Michael (Blog Owner)
October 11th, 2007 at 11:02 am
@酷辣客,这个版本已经集成了所有的修改,下载完就可以直接用了。
酷辣客
October 11th, 2007 at 11:44 am
谢谢,得确很好用,我的另一个站用他。
mo
October 21st, 2007 at 12:34 pm
这个功能可以用在2.3里面吗?
Michael (Blog Owner)
October 21st, 2007 at 12:37 pm
@mo, 可以。
Ryana
October 24th, 2007 at 11:15 am
这个我已经用上了,很棒~
不知道里面有没有什么调用函数,可以取出post的time,author,catogory的信息呢?
http://www.psytopic.com/mag/中间的列表后面跟随了时间,不是全部link的大list,感觉好很多~
先说声谢谢~
DamionKutaeff
March 23rd, 2008 at 4:42 pm
Hello everybody, my name is Damion, and I’m glad to join your conmunity,
and wish to assit as far as possible.
Tiferneenanda
April 14th, 2008 at 5:17 pm
I’d prefer reading in my native language, because my knowledge of your languange is no so well. But it was interesting! Look for some my links:
RSS feed for comments on this post · TrackBack URI
Leave a reply
Copyright © 2006-2008 巧克力工厂 - hosted at Slicehost, powered by WordPress and Nginx.
若无事先声明,本站作品匀采用署名-非商业性使用-相同方式共享 3.0 Unported进行许可。