您的位置:首页 > 其它

关于wordpress侧边栏常用功能的非插件方式实现

2011-10-15 15:06 357 查看
wordpress侧边栏通常用来放置日历、最新文章、最新评论、文章分类、友情链接等信息,利用wordpress后台侧边栏小工具可以实现一些常用的基本功能,比如日历、友情链接、分类目录等,但是一些复杂的功能比如能要直接显示最新留言、随机显示几篇文章、或者根据当前文章标签显示相关文章,wordpress自带小工具无法直接实现。

可以借助相关的插件来实现,但是插件毕竟有拖慢wordpress速度的隐患,因此,插件能不用尽量不用,并且上面所述功能都可以通过代码实现。

1,侧边栏显示最新文章,该功能实现简单,直接调用wordpress函数即可,下面的代码显示最近的10篇文章,改变函数get_archives第二个数字参数可改变显示的文章数目。

[code] <li><h2><?php _e(‘最新文章’);?></h2>


<ul>


<?php get_archives(‘postbypost’, 10);?>


</ul>


</li>

[/code]

2,侧边栏显示随机文章,随机文章使以前的文章有了从见天日的机会,下面的代码随机显示10篇文章,改变numberposts的值可改变随机显示的文章数目。

[code] <li><h2><?php _e(‘随机文章’);?></h2>


<ul>


<?php $rand_posts = get_posts(‘numberposts=10&orderby=rand’);


foreach( $rand_posts as $post ) :?>


<li>


<a href=”<?php the_permalink();?>”><?php the_title();?></a>


    </li>


<?php endforeach;?>


</ul>


</li>

[/code]

3,侧边栏显示同类文章,当读者浏览某一篇文章时,能够根据当前文章标签显示相关的文章,下面的代码显示最近的10篇文章,改变numberposts的值可改变显示的文章数目。

[code] <li><h2><?php _e(‘同类文章’);?></h2>


<ul>


<?php $posts = get_posts(‘numberposts=10&category=’. $category->term_id); foreach($posts as $post) :?>


<li>


<a href=”<?php the_permalink();?>”><?php the_title();?></a>


    </li>


<?php endforeach;?>


</ul>


</li>

[/code]

4,侧边栏显示最新评论,wordpress自带的显示评论方式很不友好,读者不能直接看到评论内容,要想显示评论内容需借助下面的代码。这段代码能显示留言者名字和具体留言内容,比wordpress自带的评论显示更加直观,这段代码来自豆瓣九点风格主题。

[code] <li><h2><?php _e(‘最新评论’);?></h2>


<ul>


<?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,20) 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 10″;


$comments = $wpdb->get_results($sql);


$output = $pre_HTML;


foreach ($comments as $comment) {


$output .= “\n<li>”.strip_tags($comment->comment_author)


.”:” . ” <a href=\”" . get_permalink($comment->ID) .


“#comment-” . $comment->comment_ID . “\” title=\”on ” .


$comment->post_title . “\”>” . strip_tags($comment->com_excerpt)


.”</a></li>”;


} $output .= $post_HTML;


echo $output;?>


</ul>


</li>

[/code]

5侧边栏显示评论最多的文章,该功能的实现仍然来自豆瓣九点主题,需要一个自定义函数get_most_comment,将该函数放在主题functions.php文件内,供主题调用。

[code] function get_most_comment($posts_num=10, $days=30){


global $wpdb;


$sql = “SELECT `ID` , `post_title` , `comment_count` FROM $wpdb->posts WHERE `post_type` = ‘post’ AND TO_DAYS( now( ) ) – TO_DAYS( `post_date` ) < $days


ORDER BY `comment_count` DESC LIMIT 0 , $posts_num “;


$posts = $wpdb->get_results($sql);


$output = “”;


foreach ($posts as $post){


$output .= “\n<li><a href= \”".get_permalink($post->ID).”\” rel=\”bookmark\” title=\”".$post->post_title.”\” >”.$post->post_title.”</a></li>”;


} echo $output; }


?>

[/code]

在侧边栏加入以下代码,即可显示热评文章。

[code] <li><h2><?php _e(‘热评推荐’);?></h2>


<ul>


         <?php get_most_comment(10,1000);?>


</ul>


</li>

[/code]

在任何需要显示相关文章的地方都可以使用这些代码,比如,可以在文章页面最下面显示相关文章,显示热评推荐等。

另外一个显示分类目录的函数也很好用,能在分类目录后面显示目录内文章数量,该函数为<?phpwp_list_pages('depth=3&title_li=<h2>Pages</h2>');?>,它通过depth控制目录级别。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐