Hi,
有钱终成眷属
没钱亲眼目睹

让NANA主题侧边栏显示最新说说心情

本文最后更新于2021年4月1日,已超过 3 年没有更新,如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!

当初,在浏览其他博友的站点时,发现侧边栏可以显示最新的说说心情,感觉很漂亮。想着,能不能在自己的博客侧边栏也显示。在自己的多次修改和测试下,终于可以显示了。

博主我使用的是懿古今的NANA主题,因此也是在此主题的基础上进行的修改适配。理论上,也适用其他WP主题,不过需要进行二次修改。
下面我将介绍下,大致的修改步骤及方法。
1、新建shuo.php,并复制下列代码后,上传至/inc/functions/文件夹下。

<?php
//原文地址:www.chyiyang.cn/52.html
function shuo($number){
  global $wpdb, $post; 
  $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'shuoshuo' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 0 , $number ";
  $posts = $wpdb->get_results($sql);
  $output = "";	
  foreach ($posts as $post){		
    $output .= "<li class=\"clr\"><div class=\"time\"><span class=\"r\">".get_the_time('d')."</span>/<span class=\"y\">".get_the_time('m月')."</span></div><div class=\"title\">".$post->post_content."</div></li>";
  }
  echo $output;	
}
?>

2、复制下列代码,粘贴到/in/functions/widgets.php最下面。

// 心情说说:www.chyiyang.cn/52.html
class shuoshuo_post extends WP_Widget {
  function __construct(){
    parent::__construct( 'shuoshuo_post', '主题&nbsp;&nbsp;心情说说', array('description' => '主题自带的心情说说小工具') );
  }
  function widget($args, $instance) {
    extract($args);
    $title = apply_filters( 'widget_title', $instance['title'] );
    echo $before_widget;
    if ( ! empty( $title ) )
      echo $before_title . $title . $after_title;
      $number = strip_tags($instance['number']) ? absint( $instance['number'] ) : 5;
?>
<div class="shuo_cat" id="shuo_cat">
   <ul>
      <?php shuo($number);wp_reset_query(); ?>
   </ul>
</div>	
<script>
   $(function () {
      $(".clr").mouseover(function () {
        $(this).addClass('hov');
       }).mouseleave(function () {
           $(this).removeClass('hov');
       });
    })
</script>
<div class="clear"></div>
<?php
   echo $after_widget;
}
   function update( $new_instance, $old_instance ) {
     if (!isset($new_instance['submit'])) {
	return false;
     }
     $instance = $old_instance;
     $instance = array();
     $instance['title'] = strip_tags( $new_instance['title'] );
     $instance['number'] = strip_tags($new_instance['number']);
     return $instance;
}
   function form($instance) {
     if ( isset( $instance[ 'title' ] ) ) {
	$title = $instance[ 'title' ];
     }
     else {
	$title = '心情?说说';
     }
     global $wpdb;
     $instance = wp_parse_args((array) $instance, array('number' => '5'));
     $number = strip_tags($instance['number']);
?>
     <p><label for="<?php echo $this->get_field_id( 'title' ); ?>">标题:</label>
     <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
     <p><label for="<?php echo $this->get_field_id('number'); ?>">显示数量:</label>
     <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p> 
     <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
<?php }
}
add_action( 'widgets_init', create_function( '', 'register_widget( "shuoshuo_post" );' ) );

3、在NANA主题functions.php下,加入如下代码。

// 说说心情
require get_template_directory() . '/inc/functions/shuo.php';

4、将下列css样式粘贴到主题下的style样式里。

shuo_cat ul{padding:8px 15px 15px}
.shuo_cat li{border-bottom:1px dotted #cbcbcb;padding:9pt 0}
.shuo_cat li .time{float:left;width:70px;height:36px;line-height:36px;text-align:center;color:#616161;font-size:9pt;background:#f2f2f2;font-family:arial}
.shuo_cat li .time span{display:inline-block}
.shuo_cat li .time span.r{font-size:22px;width:25px;margin-right:2px;text-align:right}
.shuo_cat li .time span.y{font-size:9pt;margin-left:2px;position:relative;bottom:1px}
.shuo_cat li .title{height:auto;line-height:18px;overflow:hidden;font-size:14px;color:#3f3f3f;padding-left:6pt;white-space:normal}
.shuo_cat li.hov{border-bottom:1px dotted #c01e22}
.shuo_cat li.hov .time{background:#c01e22;color:#fff}
.shuo_cat li.hov .title{color:#c01e22}
 

好了,到此就弄完了。

添加此功能,需要站点有说说页面才可实现。

后台效果

前端显示效果

温馨提示

本文为博主原创内容,转载需写明出处。

赞(0)
未经允许不得转载:伊阳博客 » 让NANA主题侧边栏显示最新说说心情

留言 3

  1.  沙发
    依晨4年前 (2020-05-23)(  Safari    Mac OSX 10_15_4 ) 来自:广东省东莞市 移动回复

    大神,如果是想在某个页面按“作者”来调用说说,请问怎么操作呢? 谢谢!

    • 伊阳4年前 (2020-05-23)(  Google Chrome    Windows 10 ) 来自:陕西省宝鸡市岐山县 联通回复

      @依晨:不好意思,没有研究过,无法回答你,望见谅~~

      • 依晨4年前 (2020-05-26)(  Safari    Mac OSX 10_15_4 ) 来自:广东省东莞市 移动回复

        @伊阳:Anyway, 还是谢谢你!