博客

  • wordpress给指定ID的分类目录分页添加不同数量文章

    wordpress给指定ID的分类目录分页添加不同数量文章

    function jzp_wp_filter_pre_get_posts($query)
    {
        if ($query->is_main_query()) { 
            $num = '';
            if (is_category(array(38))) {  
                $num = 999;
                $query->set('posts_per_page', $num); 
            }
        }
        return $query;
    }
    
    add_action('pre_get_posts', 'jzp_wp_filter_pre_get_posts');
  • wordpress子分类使用父分类模板

    子分类使用父分类模板

     function f_category_template($template){
    	$category = get_queried_object();
    	if($category->parent !='0'){
    		while($category->parent !='0'){
    			$category = get_category($category->parent);
    		}
    	}
    	
    	$templates = array();
     
    	if ( $category ) {
    		$templates[] = "category-{$category->slug}.php";
    		$templates[] = "category-{$category->term_id}.php";
    	}
    	$templates[] = 'category.php';
    	return locate_template( $templates );
    }
  • 怎么用wordpress自己创建一个网站

    网络时代搭建网站已经是一种非常必要的事情,如果你拥有一个公司,非常有必要为自己的公司搭建一个官方网站。过去建网站需要专业的程序员来做,现在只要学习一下教程,就可以自己使用开源的CMS搭建网站。

    比如,Wordpress、Joomla、Drupal开源CMS三巨头,就是很多人搭建网站的首要选择。选择好CMS系统,是自己创建网站的第一步。

    以Wordpress为例,在确定了使用它作为网站的管理系统后,就可以到专门卖Wordpress模板的专业网站上购买模板(国外的有主题森林 themeforest.net,国内的有简站Wordpress主题 jianzhanpress.com)。

    买好模板后,将模板上传到已经安装好的Wordpress主题目录中,在后台启用主题,配置调用一下,半小时之内,就可以搭建起一个简洁、实用的Wordpress网站了。

    WordPress简介

    WordPress是使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站。也可以把 WordPress当作一个内容管理系统(CMS)来使用。

    WordPress是一款个人博客系统,并逐步演化成一款内容管理系统软件,它是使用PHP语言和MySQL数据库开发的,用户可以在支持 PHP 和 MySQL数据库的服务器上使用自己的博客。

    WordPress有许多第三方开发的免费模板,安装方式简单易用。不过要做一个自己的模板,则需要你有一定的专业知识。比如你至少要懂的标准通用标记语言下的一个应用HTML代码、CSS、PHP等相关知识。

    WordPress官方支持中文版,同时有爱好者开发的第三方WordPress网站模板。WordPress拥有成千上万个各式插件和不计其数的主题模板样式,可以用这些来快速搭建起网站。

  • 一个好的wordpress外贸网站应该具备以下5个特点

    简洁

    客户是来看产品信息的,网站越简洁,产品越突出,客户最容易看到。

    实用

    外贸网站是做给客户的,功能一定不要太多,突出自己的产品即可。

    准确

    外贸网站是给外国人看的,信息必须要准确,要把网站上的每一句话,都要以对方能看得懂的语言准确无误的表达出来。有些人,为了图省钱,直接用翻译软件把自己的产品和公司信息,翻译一下就放在网站上了。这样的信息,放上去,还不如不放。你不放,客户还不知道,你有多不专业。

    速度快

    客户是没有耐心,等着看你的网站的。那么多的网站,又不是非看你的不可。所以,网站的打开速度一定要快。不能在服务器上省钱,这是最得不偿失的。简站WP主题

    即时反馈

    有些人在外贸网站上放了邮件和社交软件等联系方式,但是,客户和他联系了,回复的很慢,等看到了才回复,可能已经过去好多天了。世界那么大,同行那么多,客户不可能,只联系你一家,不要因为反馈不即时,而错失了客户。

  • wordpress禁用Rest API

    将以下代码插入到functions.php文件中即可

    add_filter("json_enabled", "__return_false");
    add_filter("json_jsonp_enabled", "__return_false");
    add_filter("rest_enabled", "__return_false");
    add_filter("rest_jsonp_enabled", "__return_false");
    remove_action("init", "rest_api_init");
    remove_action("rest_api_init", "rest_api_default_filters", 10);
    remove_action("parse_request", "rest_api_loaded");
    remove_action("wp_head", "rest_output_link_wp_head", 10);
    remove_action("template_redirect", "rest_output_link_header", 11);
    remove_action("auth_cookie_malformed", "rest_cookie_collect_status");
    remove_action("auth_cookie_expired", "rest_cookie_collect_status");
    remove_action("auth_cookie_bad_username", "rest_cookie_collect_status");
    remove_action("auth_cookie_bad_hash", "rest_cookie_collect_status");
    remove_action("auth_cookie_valid", "rest_cookie_collect_status");
     
    add_filter("rest_authentication_errors", function () {
        return new WP_Error("rest_disabled", __("The REST API on this site has been disabled."), ["status" => rest_authorization_required_code()]);
    });
  • wordpress禁用Feed

    将以下代码插入到functions.php文件中,即可禁用Feed。

    function wpjzp_feed_disabled()
    {
        wp_die("Feed已经关闭, 请访问网站<a href="" . get_bloginfo("url") . "">首页</a>!");
    }
     
    add_action("do_feed", "wpjzp_feed_disabled", 1);
    add_action("do_feed_rdf", "wpjzp_feed_disabled", 1);
    add_action("do_feed_rss", "wpjzp_feed_disabled", 1);
    add_action("do_feed_rss2", "wpjzp_feed_disabled", 1);
    add_action("do_feed_atom", "wpjzp_feed_disabled", 1);
  • wordpress禁用xmlrpc

    将以下代码插入到functions.php文件中即可

    add_filter("xmlrpc_enabled", "__return_false");
    add_filter("xmlrpc_methods", function ($methods) {
        unset($methods["pingback.ping"]);
        return $methods;
    });
  • wordpress移除wp_head不常用代码

    把不常用的代码移除,让wordpress快起来,想要非一样的感觉,可以试试。

    remove_action("wp_head", "wp_generator");
    foreach (["rss2_head", "commentsrss2_head", "rss_head", "rdf_header", "atom_head", "comments_atom_head", "opml_head", "app_head"] as $action) {
        remove_action($action, "the_generator");  //删除 head 中的 WP 版本号
    }
    remove_action("wp_head", "rsd_link");                        //删除 head 中的 RSD LINK
    remove_action("wp_head", "wlwmanifest_link");                //删除 head 中的 Windows Live Writer 的适配器?
     
    remove_action("wp_head", "feed_links_extra", 3);            //删除 head 中的 Feed 相关的link
     
    remove_action("wp_head", "index_rel_link");                //删除 head 中首页,上级,开始,相连的日志链接
    remove_action("wp_head", "parent_post_rel_link", 10);
    remove_action("wp_head", "start_post_rel_link", 10);
    remove_action("wp_head", "adjacent_posts_rel_link_wp_head", 10);
     
    remove_action("wp_head", "wp_shortlink_wp_head", 10, 0);    //删除 head 中的 shortlink
    remove_action("wp_head", "rest_output_link_wp_head", 10);    // 删除头部输出 WP RSET API 地址
     
    remove_action("template_redirect", "wp_shortlink_header", 11);        //禁止短链接 Header 标签。
    remove_action("template_redirect", "rest_output_link_header", 11);    // 禁止输出 Header Link 标签。
  • wordpress禁止多地同时登录

    wordpress禁止用户在不同地点同时登录,管理员除外。

    function pcl_user_has_concurrent_sessions()
    {
        return (is_user_logged_in() && count(wp_get_all_sessions()) > 2);
    }
     
    add_action("init", function () {
        // 除了管理员,其他人不允许多地同时登陆。
        if (!current_user_can("manage_options")) {
            if (!pcl_user_has_concurrent_sessions()) {
                return;
            }
            $newest = max(wp_list_pluck(wp_get_all_sessions(), "login"));
            $session = pcl_get_current_session();
            if ($session["login"] === $newest) {
                wp_destroy_other_sessions();
            } else {
                wp_destroy_current_session();
            }
        }
    });
  • 实现wordpress一篇文章只允许同一IP评论一次

    在使用wordpress建站时,常常遇到被垃圾留言困扰,有些通过机器发垃圾留言,一发就是成百上千条,这个很烦人,因此,有些人干脆直接在wordpress网站上把留言评论功能给关闭了。

    如果你的wordpress主题必须要使用留言评论,有一个办法可以规避这个问题,即实现wordpress一篇文章只鸡同一IP的人评论一次就可以。

    将以下代码添加到functions.php中

    // 一篇文章只允许同一IP评论一次
    //获取评论用户的ip,参考wp-includes/comment.php
    function wdp_getIP() {
      $ip = $_SERVER['REMOTE_ADDR'];
      $ip = preg_replace( '/[^0-9a-fA-F:., ]/', '', $ip );
        
      return $ip;
    }
    function wdp_only_one_comment( $commentdata ) {
      global $wpdb;
      $currentUser = wp_get_current_user();
      
      // 不限制管理员发表评论
      if(empty($currentUser->roles) || !in_array('administrator', $currentUser->roles)) {
        $bool = $wpdb->get_var("SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = ".$commentdata['comment_post_ID']."  AND (comment_author = '".$commentdata['comment_author']."' OR comment_author_email = '".$commentdata['comment_author_email']."' OR comment_author_IP = '".wdp_getIP()."') LIMIT 0, 1;");
        if($bool)
          wp_die('留言已提交,请勿重复留言。<a href="'.get_permalink($commentdata['comment_post_ID']).'">点此返回</a>');
      }
    
      return $commentdata;
    }
    add_action( 'preprocess_comment' , 'wdp_only_one_comment', 20);