wordpress首页只显示某个分类的文章/插件实现(完美实现,附插件源码)

wordpress首页只显示某个分类的文章/插件实现(完美实现)

--------------------------------------------插件源码----------------------------------------------------------------

<?php
/*
Plugin Name: Front Page Included-only Categories
Version: 0.2
Plugin URI:
Description: This version uses a comma separated list of *included* category ids.
Author: fengyqf
Author URI: http://www.path8.net/
*/

function fpe_where($where) {
// Change the $cats_to_include string to the category id you do not want to appear on the front page.
// Example:  $cats_to_include = '1, 2, 3, 4';
$cats_to_include = '22, 26';

global $wpdb, $wp_query;

if (! $wp_query->is_home || strlen($cats_to_include) == 0) {
return $where;
}

if (empty($wpdb->term_relationships))
{
$where .= " AND $wpdb->post2cat.category_id IN (" . $cats_to_include . ")";
}
else
{
$where .= " AND $wpdb->term_taxonomy.term_id IN (" . $cats_to_include . ")";
}
return $where;
}

function fpe_join($join) {
global $wpdb, $wp_query;

if (!$wp_query->is_home) {
return $join;
}
if (empty($wpdb->term_relationships))
{
$join .= " LEFT JOIN $wpdb->post2cat ON $wpdb->post2cat.post_id = $wpdb->posts.ID ";
}
else
{
if (!preg_match("/$wpdb->term_relationships/i",$join))
{
$join .=" LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) ";
}
if (!preg_match("/$wpdb->term_taxonomy/i",$join))
{
$join .=" LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id";
}
}
return $join;
}

function fpe_distinct($distinct) {
global  $wp_query;

if (! $wp_query->is_home ) {
return $distinct;
}
return "distinct";
}

add_filter('posts_join', 'fpe_join');
add_filter('posts_where', 'fpe_where');
add_filter('posts_distinct', 'fpe_distinct');

?>

-----------------------------------------插件源码 end-------------------------------------------------------------------

简介:

该插件是一个wordpress插件 Front Page Excluded Categories基本上稍做修改而成,在此感谢前人的工作。

使用方法:

1. 安装,就是wordpress插件安装,这里就不用说了。

2. 配置,该插件没有管理界面,直接编辑源代码进行配置,需要编辑修改的只有如下的一行。

$cats_to_include = '22, 26';

该行在上面源码中已经标红,等号右边是一个引号括起来的以逗号分隔的数字,可以有一个或者多个,我在这里配置了两个22与26。数字即是需要显示在首页分类的id编号,该数字可以在wordpress后台的编辑“文章分类目录”、编辑分类时,在浏览器地址栏url里查看到。

原理:

只要简单的懂得一点php/mysql程序知识,比较一个这里的代码与Front Page Excluded Categories的差异部分(上面代码里已经标绿)就明白了。

插件下载:

front-page-included-categories

极简单/简陋的wordpress插件:网站访客追踪记录

提醒:有了个新版本,照样很简陋,默认将追踪记录表放到单独的数据库里;如果表访问失败会给出简单的消息提示。 传送门: 极简单/简陋的wordpress插件:网站访客追踪记录 v0.0.2

wordpress本身没有用户访问记录的功能,甚至连文章点击次数都没有记录,当然可以借助google analytics之类的工具进行站点访问的分析,但对于极小访问量的站点来说,GA似乎显得太大材小用了,要是能够精确记录下每一个访客的活动情况,是最好不过的,但并没有找到相关的插件,所以就临时查阅资料,草草完成了这个访问活动记录的插件,功能极其简陋,今天-严格地说是昨天、现在已经1:57分,已经是"明天"了-才真正开始了解一点wp插件的开发

-------------------------------------------
插件源程序\wordpress\wp-content\plugins\fstrace\fstrace.php
 <?php
/*
Plugin Name: fs trace
Plugin URI: http://www.path8.net/
Description: This plugin will trace all visitor's visit action.
Version: 0.0.1
Author: fengyqf
Author URI: http://www.path8.net/
*/

/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

function fstrace_by_fengyqf(){
global $wpdb;
$url=mysql_escape_string(substr($_SERVER["REQUEST_URI"],0,200));
$referer=mysql_escape_string(substr($_SERVER["HTTP_REFERER"],0,200));
$cookie=mysql_escape_string(substr($_SERVER["HTTP_COOKIE"],0,250));
$client=mysql_escape_string(substr($_SERVER["HTTP_USER_AGENT"],0,200));
$ip=mysql_escape_string(substr($_SERVER["REMOTE_ADDR"],0,16));
$wpdb->query("insert into `my_wpplg_trace`(`referer`,`url`,`ip`,`client`,`cookie`)
values('{$referer}','{$url}','{$ip}','{$client}','{$cookie}')");

}
add_action('wp_footer','fstrace_by_fengyqf');

?>
-------------------------

需要有一张表存储追踪到的记录,插件本身没有创建数据表的功能,需要手工创建该表,语句如下:
-- phpMyAdmin SQL Dump
-- version 3.2.3
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2010 年 01 月 30 日 17:35
-- 服务器版本: 5.0.18
-- PHP 版本: 5.2.6

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- 数据库: `wordpress`
--

-- --------------------------------------------------------

--
-- 表的结构 `my_wpplg_trace`
--

CREATE TABLE IF NOT EXISTS `my_wpplg_trace` (
`id` int(11) NOT NULL auto_increment,
`referer` varchar(200) character set utf8 NOT NULL default '',
`url` varchar(200) character set utf8 NOT NULL default '',
`time` timestamp NOT NULL default CURRENT_TIMESTAMP,
`ip` varchar(16) character set utf8 NOT NULL default '',
`client` varchar(200) character set utf8 NOT NULL default '',
`cookie` varchar(300) character set utf8 NOT NULL default '',
PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=4 ;

本来设置的是`cookie` varchar(300),但mysql服务器是4.1版本,varchar类型只能支持到255,在服务器mysql建表时只好改成`cookie` varchar(250)  ;比较无奈,购买虚拟主机附送的数据库,比较小,而且是4.1版,还不能用phpMyAdmin3.x连接——什么时间买个大点的5.x版的

---------------------------------
以前的追踪记录程序,一并列在这里,可能有点参考价值

之前借助在页面里插入以下这样代码的JS文件,籍以记录访客的记录,但对于搜索引擎的抓取,是无能为力了

$url=addslashes($_SERVER['HTTP_REFERER']);
$time=addslashes(date('Y-m-d H-n-s'));
$ip=addslashes($_SERVER["REMOTE_ADDR"]);
$client=addslashes($_SERVER["HTTP_USER_AGENT"]);
$cookie=substr(addslashes($_SERVER["HTTP_COOKIE"]),0,500);

$trace_table='fstrace';

$link = mysql_connect($mysql_host, $mysql_user,$mysql_pwd)
or die("Could not connect: " . mysql_error());
#print ("Connected successfully");
mysql_select_db($mysql_db);
$sql="insert into `{$trace_table}` (`url`,`time`,`ip`,`client`,`cookie`) values('{$url}','{$time}','{$ip}','{$client}','{$cookie}')";
mysql_query($sql,$link);
#echo $sql.''.mysql_affected_rows($link);
mysql_close($link);

或许您在阅读这篇文章时,您的IP地址、请求页面、cookie等信息已经记录在数据库里了,呵呵.

固定链接: http://www.path8.net/tn/archives/145