站长百科 | 数字化技能提升教程 数字化时代生存宝典
首页
数字化百科
电子书
建站程序
开发
服务器
办公软件
开发教程
服务器教程
软件使用教程
运营教程
热门电子书
WordPress教程
宝塔面板教程
CSS教程
Shopify教程
导航
程序频道
推广频道
网赚频道
人物频道
网站程序
网页制作
云计算
服务器
CMS
论坛
网店
虚拟主机
cPanel
网址导航
WIKI使用导航
WIKI首页
最新资讯
网站程序
站长人物
页面分类
使用帮助
编辑测试
创建条目
网站地图
站长百科导航
站长百科
主机侦探
IDCtalk云说
跨境电商导航
WordPress啦
站长专题
网站推广
网站程序
网站赚钱
虚拟主机
cPanel
网址导航专题
云计算
微博营销
虚拟主机管理系统
开放平台
WIKI程序与应用
美国十大主机
编辑“
WordPress:Query Overview
”
人物百科
|
营销百科
|
网赚百科
|
站长工具
|
网站程序
|
域名主机
|
互联网公司
|
分类索引
跳转至:
导航
、
搜索
警告:
您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您
登录
或
创建
一个账户,您的编辑将归属于您的用户名,且将享受其他好处。
反垃圾检查。
不要
加入这个!
This article is an overview for developers of the process WordPress uses to build your blog pages, and how plugins can modify that process. It is aimed at developers of plugins that will do advanced queries and permalinks, and also at developers who want to understand WordPress better, in order to add new features to the core of WordPress or fix WordPress bugs. 这篇文章是写给开发人员的概述,关于创建博客网页的过程,以及插件怎样更改这个过程。这篇文章是写给插件开发人员的,他们做高级查询和permalinks,同时也是写给想要更深入地了解WordPress的开发人员的,想要向WordPress核心添加新的功能或者解决WordPress程序错误的开发人员。 For more details, you'll need to read the WordPress core PHP files and functions mentioned. 更多的信息,你需要阅读WordPress核心PHP文件和提及的函数。 So, here are the steps WordPress uses to decide what posts or pages to display on a page, and display them: WordPress执行下面的步骤,决定在一个网页上显示那篇文章或者那些网页,并且显示这些文章或者网页。 # When a visitor first clicks on or types a URL for a page that is part of your blog, WordPress starts by running a few core files (<tt>wp-config.php</tt>, <tt>wp-settings.php</tt>, etc.) If you are interested in the specifics of the file loading order, start at <tt>index.php</tt> and follow the chain of files as each PHP file includes/requires additional PHP files. #当访客首次点击你的博客网页或者输入你的博客中一个网页的URL时,WordPress就开始运行一些核心文件(<tt>wp-config.php</tt>, <tt>wp-settings.php</tt>, 等等。)如果你对文件载入顺序的详细信息,感兴趣,你可以从<tt>index.php</tt>开始,紧跟着一系列文件,因为每个PHP文件包含/需要额外的PHP文件。 # WordPress loads and initializes any plugins you have activated (calls the plugin <tt>init</tt> actions). #任何你激活的插件,WordPress都会载入并且对其初始化(称作插件 <tt>init</tt>活动)。 # WordPress loads the "text domain" for internationalization, and the <tt>functions.php</tt> file from the currently active theme. #为了国际化的需求,WordPress载入了"text domain",同时从当前运行的主题中,载入<tt>functions.php</tt>文件。 # WordPress runs the <tt>wp()</tt> function (in <tt>wp-includes/functions.php</tt>), which calls <tt>$wp->main()</tt> (<tt>$wp</tt> is an object of class <tt>WP</tt>, which is defined in <tt>wp-includes/classes.php</tt>). This tells WordPress to: #WordPress运行<tt>wp()</tt>函数(在<tt>wp-includes/functions.php</tt>中),调用<tt>$wp->main()</tt> (<tt>$wp</tt> is an object of class <tt>WP</tt>, 定义在<tt>wp-includes/classes.php</tt>中)。这命令WordPress: ## Parse the URL into a query specification using <tt>WP->parse_request()</tt> -- more on that below. ##使用<tt>WP->parse_request()</tt>将URL解析为查询specification—下面有更多的内容。 ## Set all the <tt>is_</tt> variables that are used by [[WordPress:Conditional Tags]] using <tt>$wp_query->parse_query()</tt> (<tt>$wp_query</tt> is an object of class <tt>WP_Query</tt>, which is defined in <tt>wp-includes/query.php</tt>). Note that in spite of this function's name, in this case <tt>WP_Query->parse_query</tt> doesn't actually do any parsing for us, since that is done before-hand by <tt>WP->parse_request()</tt>. ##使用<tt>$wp_query->parse_query()</tt>(<tt>$wp_query</tt> is an object of class <tt>WP_Query</tt>, 定义在<tt>wp-includes/query.php</tt>中),设置[[WordPress:Conditional Tags|条件式标签]]使用的所有参数。注意尽管函数的名称是那样的,在这个例子中,<tt>WP_Query->parse_query</tt>不为我们做任何解析,因为解析是<tt>WP->parse_request()</tt>预先执行的。 ## Convert the query specification into a MySQL database query, and run the database query to get the list of posts, in function <tt>WP_Query->get_posts()</tt>. Save the posts in the <tt>$wp_query</tt> object to be used in the WordPress Loop. ##将查询specification转换为MySQL数据库查询,运行数据库查询得到函数<tt>WP_Query->get_posts()</tt>中文章的列表。将文章保存到用在WordPress Loop中的<tt>$wp_query</tt> object。 ## Handle 404 errors. ##处理404错误。 ## Send the blog's HTTP headers. ##发送博客的HTTP标头。 ## Set up some variables for the WordPress Loop. ##为WordPress Loop设置一些变数。 # WordPress loads your template, figures out which template file to use according to the [[WordPress:Template Hierarchy]], and runs that file (basically doing whatever your template file says to do). Or, WordPress could run one of the feed files (such as <tt>wp-rss2.php</tt>) instead. #WordPress载入你的模板,根据[[WordPress:Template Hierarchy|模板层级]]决定使用哪个模板文件,并且运行那个文件(基本上是执行模板文件的命令)。或者WordPress可以运行其中的一个feed文件(例如<tt>wp-rss2.php</tt>)。 # Generally, the template or feed file runs the [[WordPress:The Loop|WordPress Loop]] to print blog posts or a static page. #一般来说,模板或者feed文件运行[[WordPress:The Loop|WordPress Loop]]打印博客文章或者静态网页。 # The template or feed file will also likely print out permalinks to some archives, categories, or posts using built-in WordPress functions. #模板或者feed文件也有可能使用内置WordPress函数,为一些归档,类别,后者文章打印permalinks。 === More on <tt>WP->parse_request()</tt> === === 更多关于 <tt>WP->parse_request()</tt>的内容 === As mentioned above, <tt>WP->parse_request()</tt> (part of class <tt>WP</tt> in <tt>wp-includes/classes.php</tt>) parses a URL into a query specification. Here is a summary of the steps it uses to do this: 如上所述,<tt>WP->parse_request()</tt> (class <tt>WP</tt> in <tt>wp-includes/classes.php</tt>的一部分)将URL解析为查询specification。下面是这个步骤的总结: # Strips the GET variable section out of the URL (i.e. anything after a "?" in the URL). Also strips out the blog's home URL. #去除URL中的得到变数部分(例如,URL中"?"后的任何内容)。同时去除博客主页URL。 # Obtains the '''rewrite rules''' that are currently in effect, by calling <tt>$wp_rewrite->wp_rewrite_rules()</tt> (<tt>$wp_rewrite</tt> is an object of class <tt>WP_Rewrite</tt>, which is defined in <tt>wp-includes/rewrite.php</tt>). The rewrite rules are basically a set of pattern matching rules for WordPress permalinks, with a specification of what to do if the pattern matches. For instance, by default there is a rule that would match a stripped permalink like <tt>category/abc</tt>, and its specification says that it means the "abc" category was requested. There is also a rewrite rule for the home page (nothing after the blog URL). # 通过调用<tt>$wp_rewrite->wp_rewrite_rules()</tt> ,得到当前有效的'''rewrite rules''' (<tt>$wp_rewrite</tt>是an object of class <tt>WP_Rewrite</tt>, 定义在<tt>wp-includes/rewrite.php</tt>中)。重写规则基本上是一组WordPress permalinks的模式匹配规则,规定了模式匹配时,应该做什么。例如,默认情况下,有一个规则会匹配stripped permalink像 <tt>category/abc</tt>, 而且规定的意思是需要"abc" 类别。主页也有一个重写规则(博客URL后什么内容也没有)。 # Goes through the rewrite rules in order, until it finds a match between a rewrite rule and the permalink. If nothing is found, it's a 404 error. If a match is found, WordPress extracts the information according to the rule specification. #按顺序查看重写规则,知道找到重写规则和permalink之间的一个匹配。如果什么匹配也没有发现,返回404错误。如果找到了匹配,WordPress根据规则,提取信息。 # Obtains the list of '''query variables''' that is currently in effect. For each query variable, WordPress checks to see if it has been set by permalink parsing, POST submission, or GET submission, and if so, WordPress saves the variable's value into the '''query specification''' array (<tt>$wp->query_vars</tt>, part of class <tt>WP</tt> in <tt>wp-includes/classes.php</tt>). #得到当前有效的一列'''查询变数'''。对于每个查询变数,WordPress检查看这个变数是否由permalink 解析,文章递交,或者得到递交设置了,如果已经设置了,WordPress将变数的值保存到'''query specification'''数组(<tt>$wp->query_vars</tt>, class <tt>WP</tt> in <tt>wp-includes/classes.php</tt>的一部分)。 === What Plugins can Modify === === 插件可以更改什么 === Here is an overview of the things a plugin can do to modify the default query and permalink behavior described above. Many of these modifications are described (with examples) in the article [[WordPress:Custom Queries]]. 下面是一个概述,关于插件可以怎样做,更改默认查询和上述的permalink。许多这样的更改在[[WordPress:Custom Queries|自定义查询]]中有描述(带有例子)。 * Add, modify, or remove rewrite rules, to affect how permalinks are parsed. This is generally not done with filters and actions, but instead by calling functions in <tt>wp-includes/rewrite.php</tt>, such as <tt>add_rewrite_rule</tt>, <tt>add_rewrite_endpoint</tt>, etc. This can be a bit tricky, because <tt>WP_Rewrite->wp_rewrite_rules()</tt> usually just gets the previously-saved set of rewrite rules (they are saved in the WordPress database as option "rewrite_rules"). So if you want to modify rewrite rules, you will need to call <tt>$wp_rewrite->flush_rules()</tt> to force them to recalculate. You'll need to do this in your plugin's <tt>init</tt> action, so that it happens early enough in the process. *添加,更改,或者移除重写规则,会影响permalinks的解析方式。这些通过不是过滤器或者actions完成的,而是通过调用<tt>wp-includes/rewrite.php</tt>中的函数,例如<tt>add_rewrite_rule</tt>, <tt>add_rewrite_endpoint</tt>,等等。这样做有些微妙,因为<tt>WP_Rewrite->wp_rewrite_rules()</tt>通常只得到先前保存的改写规则(这些规则保存在 WordPress 数据库作为选项"rewrite_rules")。因此,如果你想要更改改写规则,你需要在你的插件的<tt>init</tt>操作中更改,因此更改过程很早就发生了。 * Add or remove query variables, to affect which variables are saved in the query specification from POST, GET, and permalink requests (<tt>query_vars</tt> filter). *添加或者移除查询变数,影响哪个变数保存在来自文章,GET和permalink请求(<tt>query_vars</tt> filter)的查询specification。 * Modify the query specification, after variable values are saved (<tt>request</tt> filter or <tt>parse_request</tt> action; if you want to use conditional tag tests, use the <tt>parse_query</tt> or <tt>pre_get_posts</tt> action, as these run after the <tt>is_</tt> variables are set). *变数值保存后,更改query specification(<tt>request</tt> 过滤器或者<tt>parse_request</tt> action; 如果你想使用条件式标签测试,请使用 <tt>parse_query</tt> 或者 <tt>pre_get_posts</tt> action, 因为<tt>is_</tt> 变数设置好之后,这些才会运行)。 * Modify the MySQL database query, after it is created from the query specification (<tt>posts_where</tt>, <tt>posts_join</tt>, <tt>posts_groupby</tt>, <tt>posts_orderby</tt>, <tt>posts_distinct</tt>, <tt>posts_fields</tt>, <tt>post_limits</tt>, <tt>posts_where_paged</tt>, <tt>posts_join_paged</tt>, and <tt>posts_request</tt> filters). *MySQL数据库query从query specification中创建好之后,更改MySQL数据库query(<tt>posts_where</tt>, <tt>posts_join</tt>, <tt>posts_groupby</tt>, <tt>posts_orderby</tt>, <tt>posts_distinct</tt>, <tt>posts_fields</tt>, <tt>post_limits</tt>, <tt>posts_where_paged</tt>, <tt>posts_join_paged</tt>, 和<tt>posts_request</tt> 过滤器)。 * Modify the results of the database query (<tt>the_posts</tt> filter). *更改数据库query的结果(<tt>the_posts</tt> 过滤器)。 * Override the default template file choice (<tt>template_redirect</tt> action). *废除默认模板文件选择(<tt>template_redirect</tt> action)。
摘要:
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅
Wordpress-mediawiki:版权
的细节)。
未经许可,请勿提交受版权保护的作品!
取消
编辑帮助
(在新窗口中打开)