WordPress:最简单的显示最新文章的方法

2008年05月27日

分类: 如之奈何,部落一格

网志的侧栏里一般都会显示一下最近发表的文章,以方便读者了解最新的动态。在以前不懂代码的时候还到处找插件,而最近发布的一些主题一般都已经具备了这一功能。

这里介绍一个利用wp_get_archives()函数来显示最新文章列表的方法。非常简单:

  1. <?php wp_get_archives('title_li=&type=postbypost&limit=10'); ?>

limit=后面的值可以自行更改以显示相应数量的最新文章。

[via ThemeLab]

[Update]: 再加一个显示最新留言的,可将pingback排除在外:

  1. <?php
  2. global $wpdb;
  3.  
  4.     $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
  5.     comment_post_ID, comment_author, comment_date_gmt, comment_approved,
  6.     comment_type,comment_author_url,
  7.     SUBSTRING(comment_content,1,50) AS com_excerpt
  8.     FROM $wpdb->comments
  9.     LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
  10.     $wpdb->posts.ID)
  11.     WHERE comment_approved = '1' AND comment_type = '' AND
  12.     post_password = ''
  13.     ORDER BY comment_date_gmt DESC LIMIT 5";
  14.  
  15.     $comments = $wpdb->get_results($sql);
  16.     $output = $pre_HTML;
  17.     $output .= "\n
  18. <ul>";
  19.  
  20.     foreach ($comments as $comment) {
  21.     $output .= "\n
  22. <li>"."<a href=\"" . get_permalink($comment->ID) .
  23.     "#comment-" . $comment->comment_ID . "\" title=\"on " .
  24.     $comment->post_title . "\">" .strip_tags($comment->comment_author)
  25.     .":<br/>
  26. <div>" . strip_tags($comment->com_excerpt)
  27.     ."</div>
  28.  
  29. </a></li>
  30.  
  31. ";
  32.     }
  33.  
  34.     $output .= "\n</ul>
  35.  
  36. ";
  37.     $output .= $post_HTML;
  38.  
  39. echo $output; ?>

在这段代码中还可以设置留言摘要的字数,当然也可以显示多少个最新的留言。

[via and slightly modified by lucifr]

分享或收藏本文:
  • Twitter
  • Buzz
  • Google Bookmarks
  • del.icio.us
  • 收藏到鲜果
  • QQ书签
  • 豆瓣九点
  • 豆瓣
  • 新浪微博
  • 人人网
  • 开心网
  • DigLog
  • 百度搜藏
  • Live
  • Yahoo! Bookmarks
  • Tumblr
  • Facebook
  • Add to favorites
  • email

{ 14 条评论… 阅读评论或参加讨论 }

1 yacca 2008年05月27日 at 13:34

其实侧边栏都可以用代码写 都是调用么

Reply

2 Lucifr 2008年05月28日 at 01:39

嗯,整个网页说白了也都是代码嘛,呵呵

Reply

3 Buzzurls 2008年05月30日 at 17:16

好文,感谢分享。

Reply

4 Lucifr 2008年05月31日 at 00:19

@Buzzurls: 共同学习,共同学习……

Reply

5 xy 2008年08月07日 at 21:16

请问下,如果我要在PNG图片上利用PHP函数生成标题,那么函数是不是也是这样呢?
怎么去掉?在图片上看的不美观了。。

Reply

6 Lucifr 2008年08月07日 at 22:16

没太懂你的意思,是不是这样的?
<?php the_title_attribute(); ?/>” src=”

Reply

7 xy 2008年08月08日 at 12:58

不是,是这样的
http://blog.djmax.in/stat/signture.png
得到最新标题,然后输出到图片

Reply

8 Lucifr 2008年08月11日 at 23:18

lucifr学浅,还真不知道是怎么弄的,XY知道了一定要告诉我啊

Reply

9 xy 2008年08月12日 at 16:47

这几天终于弄会了,很多细节,我记下来了,你可以参考参考~~

Reply

10 xy 2008年08月12日 at 16:47
11 huar 2009年09月18日 at 19:46

弄不了在新窗口中打开啊……

Reply

12 sf 2009年12月08日 at 23:20

不知道为什么调用了只显示一条……

Reply

13 言情小说 2010年07月16日 at 21:11

中间代码那段是怎么做的, 怎么这么漂亮

Reply

14 Lucifr 2010年07月16日 at 21:16

是用的wordpress的一个代码高亮插件

Reply

发表评论

前一篇文章:

后一篇文章:

唾沫新星

唾沫新星

  • Lifestream