站长百科 | 数字化技能提升教程 数字化时代生存宝典
首页
数字化百科
电子书
▼
建站程序
开发
服务器
办公软件
开发教程
▼
服务器教程
软件使用教程
运营教程
热门电子书
▼
CSS教程
WordPress教程
导航
程序频道
推广频道
网赚频道
人物频道
网站程序
网页制作
云计算
服务器
CMS
论坛
网店
虚拟主机
cPanel
网址导航
WIKI使用导航
WIKI首页
热点词条
最新资讯
网站程序
站长人物
页面分类
使用帮助
编辑测试
创建条目
网站地图
站长百科导航
站长百科
主机侦探
IDCtalk云说
跨境电商导航
WordPress啦
站长专题
网站推广
网站程序
网站赚钱
虚拟主机
cPanel
网址导航专题
云计算
微博营销
虚拟主机管理系统
开放平台
WIKI程序与应用
美国十大主机
编辑“
WordPress:Custom Queries
”(章节)
人物百科
|
营销百科
|
网赚百科
|
站长工具
|
网站程序
|
域名主机
|
互联网公司
|
分类索引
跳转至:
导航
、
搜索
警告:
您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您
登录
或
创建
一个账户,您的编辑将归属于您的用户名,且将享受其他好处。
反垃圾检查。
不要
加入这个!
=== 自定义归档 === To continue with the geo-tagging plugin from the last example, let's assume we want the plugin to enable custom permalinks of the form <tt>www.example.com/blog?geostate=oregon</tt> to tell WordPress to find posts whose state matches "oregon" and display them. 继续前一个例子中的geo-tagging插件,加入我们希望插件允许形式为<tt>www.example.com/blog?geostate=oregon</tt>的自定义permalinks,指示WordPress查找州名为"俄勒冈州"的文章并且显示这些文章。 To get this to work, the plugin must do the following: 要实现这个操作,插件必须执行: * Ensure that when WordPress parses the URL, the state gets saved in the query variables; to do this, we have to add "geostate" to the list of query variables WordPress understands (<tt>query_vars</tt> filter). Here's how to do that: *确定WordPress解析URL的时候,州名在查询变数中保存了;要达到这个目的,我们需要将"geostate"添加到WordPress识别的查询变数列表上(<tt>query_vars</tt> filter)。下面是操作方法: <pre> add_filter('query_vars', 'geotag_queryvars' ); function geotag_queryvars( $qvars ) { $qvars[] = 'geostate'; return $qvars; } </pre> <pre> add_filter('query_vars', 'geotag_queryvars' ); function geotag_queryvars( $qvars ) { $qvars[] = 'geostate'; return $qvars; } </pre> * Do the right query when the "geostate" query variable is found; this is similar to the custom queries discussed in the previous examples. The only difference is that instead of testing for <tt>is_search</tt> or something similar in <tt>posts_where</tt> and other database query filters, we'll instead test to see whether the "geostate" query variable has been detected. Here is the code to replace the <tt>if( is_search() )</tt> statements in the examples above: *找到"geostate"查询变数的时候,正确地查询;这与上述例子中讨论的自定义查询相似。唯一的不同在于,我们没有测试<tt>is_search</tt>或者<tt>posts_where</tt>和其它数据库查询filters中相似的内容,而是测试是否查看到"geostate"查询变数。下面是替换上面的例子中<tt>if( is_search() )</tt>声明的代码: <pre> global $wp_query; if( isset( $wp_query->query_vars['geostate'] )) { // modify the where/join/groupby similar to above examples } </pre> <pre> global $wp_query; if( isset( $wp_query->query_vars['geostate'] )) { // modify the where/join/groupby similar to above examples } </pre> * Probably the plugin also needs to generate these permalinks. For instance, the plugin might have a function called <tt>geotags_list_states</tt> that would find out which states exist in its geotag table, and make links to them: *也许插件也需要创建这些permalinks。例如,插件可能有个称为<tt>geotags_list_states</tt>的函数,这个函数会查找geotag表格中有哪些州,并且给这些州创建链接: <pre> function geotags_list_states( $sep = ", " ) { global $geotag_table, $wpdb; // find list of states in DB $qry = "SELECT geotag_state FROM $geotag_table " . " GROUP BY geotag_state ORDER BY geotag_state"; $states = $wpdb->get_results( $qry ); // make list of links $before = '<a href="' . get_bloginfo('home') . '?geostate='; $mid = '">'; $after = "</a> "; $cur_sep = ""; foreach( $states as $row ) { $state = $row->state; echo $cur_sep . $before . rawurlencode($state) . $mid . $state . $after; // after the first time, we need separator $cur_sep = $sep; } } </pre> <pre> function geotags_list_states( $sep = ", " ) { global $geotag_table, $wpdb; // 在DB中找到州列表 $qry = "SELECT geotag_state FROM $geotag_table " . " GROUP BY geotag_state ORDER BY geotag_state"; $states = $wpdb->get_results( $qry ); // 创建链接列表 $before = '<a href="' . get_bloginfo('home') . '?geostate='; $mid = '">'; $after = "</a> "; $cur_sep = ""; foreach( $states as $row ) { $state = $row->state; echo $cur_sep . $before . rawurlencode($state) . $mid . $state . $after; // 第一次后,我们需要分隔符 $cur_sep = $sep; } } </pre>
摘要:
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅
Wordpress-mediawiki:版权
的细节)。
未经许可,请勿提交受版权保护的作品!
取消
编辑帮助
(在新窗口中打开)