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

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

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

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

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

[via ThemeLab]

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

<?php
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,
    SUBSTRING(comment_content,1,50) AS com_excerpt
    FROM
$wpdb->comments
    LEFT OUTER JOIN
$wpdb->posts ON ($wpdb->comments.comment_post_ID =
    
$wpdb->posts.ID)
    WHERE comment_approved = '1' AND comment_type = '' AND
    post_password = ''
    ORDER BY comment_date_gmt DESC LIMIT 8
";
 
    
$comments = $wpdb->get_results($sql);
    
$output = $pre_HTML;
    
$output .= "\n
<ul>
";
 
    
foreach ($comments as $comment) {
    
$output .= "\n
<li>
"."<a href=\"" . get_permalink($comment->ID) .
    
"#comment-" . $comment->comment_ID . "\" title=\"on " .
    
$comment->post_title . "\">" .strip_tags($comment->comment_author)
    .
":
" . strip_tags($comment->com_excerpt)
    .
"
 
</a></li>
 
";
    
}
 
    
$output .= "\n</ul>
 
";
    
$output .= $post_HTML;
 
echo $output; ?>

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

[via and slightly modified by lucifr]

点击查看更多关于的内容。

17fav 收藏本文
1 Star2 Stars3 Stars4 Stars5 Stars (3人投票, 平均 3.67颗星,
Loading ... Loading ...

10 Comments

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

[回复]

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

[回复]

好文,感谢分享。

[回复]

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

[回复]

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

[回复]

Lucifr 回复于 August 7th, 2008 10:16 pm:

没太懂你的意思,是不是这样的?

  1. <img alt="<?php the_title_attribute(); ?>" src="">

[回复]

xy 回复于 August 8th, 2008 12:58 pm:

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

Lucifr 回复于 August 11th, 2008 11:18 pm:

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

xy 回复于 August 12th, 2008 4:47 pm:

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

xy 回复于 August 12th, 2008 4:47 pm:

Leave a Comment

 

收藏 & 分享

Powered by 17fav.com