Back End

WP主题二开研究社

PineappleCat · 3月21日 · 2021年 134次已读

此专栏的开设的意义:二开WP主题,不是修改原主题,而是为了让它更美!

①.我叫先登录后看文章?

作用:游客可以正常访问主页,当点击文章时,若没有登录自动调整到登录页面,若已经登录则可查看文章。

在主题的function.php中添加

function login_if_not(){
    if (is_single()) {
        if(!is_user_logged_in()){
            auth_redirect();
        }
    }
}

在主题的header.php最上面添加

<?php login_if_not();?>

②.我是右下角的提示框

在主题的function.php中添加

if(!wp_is_mobile()){
    add_action( 'wp_head', 'show_addr' );
}

function show_addr(){
echo "<div id=\"welcome\">
    <center class=\"from-url\">右下侧的FAQ➡</center>
    <center class=\"from-url\">会让你快捷使用本站</center>
    <div class=\"closebox\"><a style=\"color:#dd8d8d\" href=\"javascript:void(0)\"onclick=\"$('#welcome'). slideUp('slow');$('.closebox').css('display','none');\" title=\"关闭提示\">关闭提示</a></div>
    </div>";
}

添加CSS代码(主题内找个地方加上或者在自定义里面有个设置css的地点加上)

/*右下角欢迎*/
#welcome {
    background:#ffffff;
    border:0px solid #ffffff;
    color:#000000;
    font-size:14px;
    opacity:0.7;
    filter:alpha(opacity=70);
    padding:10px 20px;
    position:fixed;
    right:45px;
    bottom:75px;
    z-index:99999;
    border-top-left-radius: 15px;
    border-bottom-right-radius: 15px;
    border-top-right-radius: 15px;
    border-bottom-left-radius: 15px;
    }
.from-url{color:#20A0FF;}
.closebox{
    float:center;
    text-align:center;
    font-size:16px;
    margin-top:10px;}

③.让标签云更美丽

本博客使用的是Simple Tags插件,搜一搜安装上就可以了,设置全默认。

要使用的话需要添加侧边栏小工具。我的小工具设置如下:

但是出来的还是很丑! 开始修改样式了,在自定义里面有个设置css的地点加上。

/*云标签*/
.st-tags{
    transition:.25s;
    transition-property: all;
    transition-duration: 0.25s;
    transition-timing-function: ease;
    transition-delay: 0s;
}
.st-tags:link, .st-tags:visited {
  background-color: white;
  padding:8px 10px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
   border-radius:7px;
  box-shadow:0 13px 15px rgb(0 0 0 / 10%);
}
.st-tags:hover, .st-tags:active {
  background-color: #20ADFF;
  box-shadow: none;
}

④.自定义展示网站数据

在主题目录下创建WordPressRunningInfoStat.php的文件,添加下面代码:

<?php

// 定义小工具的类 WordPressRunningInfoStat
class WordPressRunningInfoStat extends WP_Widget{

	function WordPressRunningInfoStat(){
		// 定义小工具的构造函数
		$widget_ops = array('classname' => 'widget_blogstat', 'description' => '显示博客的统计信息');
		$this->WP_Widget(false, '博客统计', $widget_ops);
	}
	
	function form($instance){
		// 表单函数,控制后台显示
		// $instance 为之前保存过的数据
		// 如果之前没有数据的话,设置默认量
		$instance = wp_parse_args(
			(array)$instance,
			array(
				'title' => '博客统计',
				'establish_time' => '2016-09-28' 
			)
		);
		$title = htmlspecialchars($instance['title']);
		$establish_time = htmlspecialchars($instance['establish_time']);
		// establish_time => 建站日期
		
		// 表格布局输出表单
		$output = '<table>';
		$output .= '<tr><td>标题</td><td>';
		$output .= '<input id="'.$this->get_field_id('title') .'" name="'.$this->get_field_name('title').'" type="text" value="'.$instance['title'].'" />';
		$output .= '</td></tr><tr><td>建站日期:</td><td>';   
		$output .= '<input id="'.$this->get_field_id('establish_time') .'" name="'.$this->get_field_name('establish_time').'" type="text" value="'.$instance['establish_time'].'" />';   
		$output .= '</td></tr></table>';  
		echo $output;   
	}
	
	function update($new_instance, $old_instance){
		// 更新数据的函数
		$instance = $old_instance;
		// 数据处理
		$instance['title'] = strip_tags(stripslashes($new_instance['title']));
		$instance['establish_time'] = strip_tags(stripslashes($new_instance['establish_time']));
		return $instance;
	}
	
	function widget($args, $instance){
		extract($args); //展开数组
		$title = apply_filters('widget_title',empty($instance['title']) ? '&nbsp;' : $instance['title']);
		$establish_time = empty($instance['establish_time']) ? '2013-01-27' : $instance['establish_time'];
		echo $before_widget;
		echo $before_title . $title . $after_title;
		echo '<div style="margin-top: 15px;"><section class="widget widget_categories wrapper-md clear"><ul class="list-group">';
		$this->efan_get_blogstat($establish_time);
		echo '</ul></section></div>';
		echo $after_widget;
	}
	
	function efan_get_blogstat($establish_time /*, $instance */){
		// 相关数据的获取
		global $wpdb;
		$count_posts = wp_count_posts();
		$published_posts = $count_posts->publish;
		$draft_posts = $count_posts->draft;
		$comments_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments");
		$time = floor((time()-strtotime($establish_time))/86400);
		$count_tags = wp_count_terms('post_tag');
		$count_pages = wp_count_posts('page');
		$page_posts = $count_pages->publish;
		$count_categories = wp_count_terms('category'); 
		$link = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'"); 
		$users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
		$last = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')");
		$last = date('Y-m-d', strtotime($last[0]->MAX_m));
		$total_views = $wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = 'views'");  
        
		$uptime = trim(file_get_contents('/proc/uptime'));
		$uptime = explode(' ', $uptime);
		$uptime = $uptime[0];
		$uptime = round($uptime / 86400, 2);

       
        
		$this->statItem('fas fa-file-alt', 'Articles', $published_posts . ' 篇');
		$this->statItem('far fa-clock', 'Safe Operation', (int)($time/365) . '年 '.(int)($time%365).'天');
		$this->statItem('fas fa-sync-alt', 'Last Update', $last);
		$this->statItem('fas fa-blog', 'Construction Date', $establish_time);
		$this->statItem('fas fa-eye', 'Total Views', (int)($total_views/10000) . '万 ' .(int)($total_views%10000) . '次');
		/*$this->statItem('fas fa-comments', 'Comments', $comments_count);*/
		/*$this->statItem('fas fa-tags', 'Label', $count_tags);*/
		/*$this->statItem('fas fa-power-off', '自上次服务重启后持续运行', $uptime . ' 天');*/
	}

	function statItem($fa, $desc, $data) {
		echo '<li class="list-group-item">';
		echo '<i class="' . $fa . '"></i>';
		echo '<span class="badge pull-right">' . $data . '</span>';
		echo ' ' . $desc. ' ';
		echo '</li>';
	}
}

function WordPressRunningInfoStat(){
	// 注册小工具
	register_widget('WordpressRunningInfoStat');
}

add_action('widgets_init','WordpressRunningInfoStat');

?>

在主题的 function.php文件中添加

include("WordpressRunningInfoStat.php");

在自定义里面的设置css添加

/*给博客统计加背景变透明*/
.list-group-item{
    background-color:transparent;
}

离大功告成还剩一步,在小工具在添加博客统计:

啦啦♪(^∇^*)..就成这样了

⑤.登录与未登录显示不同菜单栏

先进入WordPress后台 – 外观 – 菜单中, 创建两个菜单,菜单名称分别设置成:已登录 和 未登录,并给这两个菜单分配不同的菜单项。先将这两个中的一个设置为主菜单,保证有一个先显示出来。

开始下一步前,我们需要知道菜单位置名称,也就是theme_location参数的值,用文本编辑器打开当前主题目录中的header.php,搜索wp_nav_menu一般都能找到这个值,代码一般长这样:

// theme_location后面的 top 就是我们要找的值
wp_nav_menu( array( 'theme_location' => 'top', 'menu_id' => 'top-menu') );

也有可能菜单位置名称不在这个文件里面,但是可以通过这个文件去找对应位置的文件。

最后,在当前主题的functions.php添加以下代码即可:

function ludou_nav_menu_args($args = '') {
   // 下面的top改成第2步获取到的菜单位置名称
   if ($args['theme_location'] == 'top') {
      if (is_user_logged_in()) {
         $args['menu'] = '已登录';
      }
      else {
         $args['menu'] = '未登录';
      }
   }
   
   return $args;
}

add_filter('wp_nav_menu_args', 'ludou_nav_menu_args');

Click here to view the copyright notice of this site(点击此处查看本站版权声明)
0 条回应

必须 注册 为本站用户, 登录 后才可以发表评论!