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

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

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

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

   1: <li><h2><?php _e(‘最新文章’);?></h2>
   2:     <ul>
   3:         <?php get_archives(‘postbypost’, 10); ?>
   4:     </ul>
   5: </li>

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

   1: <li><h2><?php _e(‘随机文章’);?></h2>
   2:     <ul>
   3:         <?php $rand_posts = get_posts(‘numberposts=10&orderby=rand’);
   4:         foreach( $rand_posts as $post ) :?>
   5:         <li>
   6:             <a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a>
   7:         </li>
   8:         <?php endforeach; ?>
   9:     </ul>
  10: </li>

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

   1: <li><h2><?php _e(‘同类文章’);?></h2>
   2:     <ul>
   3:         <?php $posts = get_posts(‘numberposts=10&category=’. $category->term_id); foreach($posts as $post) : ?>
   4:         <li>
   5:             <a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a>
   6:         </li>
   7:         <?php endforeach; ?>
   8:     </ul>
   9: </li>

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

   1: <li><h2><?php _e(‘最新评论’);?></h2>
   2: <ul>
   3: <?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
   4: FROM $wpdb->comments
   5: LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
   6: WHERE comment_approved = ’1′ AND comment_type = ” AND
   7: post_password = ”
   8: ORDER BY comment_date_gmt DESC
   9: LIMIT 10″;
  10: $comments = $wpdb->get_results($sql);
  11: $output = $pre_HTML;
  12: foreach ($comments as $comment) {
  13: $output .= “\n<li>”.strip_tags($comment->comment_author)
  14: .”:” . ” <a href=\”" . get_permalink($comment->ID) .
  15:#comment-” . $comment->comment_ID . “\” title=\”on ” .
  16: $comment->post_title . “\”>” . strip_tags($comment->com_excerpt)
  17: .”</a></li>”;
  18: } $output .= $post_HTML;
  19: echo $output; ?>
  20: </ul>
  21: </li>

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

   1: function get_most_comment($posts_num=10, $days=30){  
   2: global $wpdb;  
   3: $sql = “SELECT `ID` , `post_title` , `comment_count` FROM $wpdb->posts WHERE `post_type` = ‘post’ AND TO_DAYS( now( ) ) – TO_DAYS( `post_date` ) < $days 
   4: ORDER BY `comment_count` DESC LIMIT 0 , $posts_num “;  
   5: $posts = $wpdb->get_results($sql);  
   6: $output = “”;  
   7: foreach ($posts as $post){  
   8: $output .= “\n<li><a href= \”".get_permalink($post->ID).”\” rel=\”bookmark\” title=\”".$post->post_title.”\” >”.$post->post_title.”</a></li>”;  
   9: } echo $output; }
  10: ?>

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

   1: <li><h2><?php _e(‘热评推荐’);?></h2>
   2:     <ul>
   3:         <?php get_most_comment(10,1000); ?>
   4:     </ul>
   5: </li>

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

另外一个显示分类目录的函数也很好用,能在分类目录后面显示目录内文章数量,该函数为<?phpwp_list_pages('depth=3&title_li=<h2>Pages</h2>');?>,它通过depth控制目录级别。

作者:codee
文章千古事,得失寸心知。


原文地址:https://www.cnblogs.com/bimgoo/p/2503245.html