WIKI使用導(dǎo)航
站長(zhǎng)百科導(dǎo)航
站長(zhǎng)專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢(qián)
- 虛擬主機(jī)
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計(jì)算
- 微博營(yíng)銷(xiāo)
- 虛擬主機(jī)管理系統(tǒng)
- 開(kāi)放平臺(tái)
- WIKI程序與應(yīng)用
- 美國(guó)十大主機(jī)
編輯“WordPress:Displaying Posts Using a Custom Select Query”
該編輯可以被撤銷(xiāo)。 請(qǐng)檢查下面的對(duì)比以核實(shí)您想要撤銷(xiāo)的內(nèi)容,然后發(fā)布下面的更改以完成撤銷(xiāo)。
最后版本 | 您的文本 | ||
第1行: | 第1行: | ||
== | == Description == | ||
At some point in your WordPress development career you may be presented with the need to display one or more posts using [[WordPress:Wikipedia:SELECT|SELECT]] criteria not provided by WordPress' [[WordPress:Template Tags/query posts | query_posts]] architecture.? For instance, it may become necessary to [[WordPress:Wikipedia:JOIN|JOIN]] WordPress tables to determine which posts should be displayed, or you may want to use data stored in your own tables to determine which [[WordPress:Writing Posts|posts]] should be displayed. | |||
The practical example, outlined below, demonstrates a process of selecting all posts with a particular [[WordPress:Using Custom Fields | Custom Field]] value stored, and displaying them in a [[WordPress:Pages|Page]] based on a [[WordPress:Pages#Creating_your_own_Page_Templates|Page Template]]. Originally, this code was used to implement a post tagging [[WordPress:Plugins|plugin]], which allowed organizing posts in less structured collections than the WordPress [[WordPress:Manage_Categories_SubPanel|Categories]]. Your own usage may be very different, but the content and example should still give you a useful introduction to the general process involved. | |||
=== | === Assumptions made in this Article === | ||
? | Generally, this article assumes you have a working knowledge of [[WordPress:Glossary#PHP|PHP]], [[WordPress:Glossary#MySQL|MySQL]], and WordPress capabilities. | ||
? | |||
? | |||
? | |||
? | |||
? | |||
? | |||
? | |||
Specific assumptions for the example, however, are: | |||
* You have at least one post with [[WordPress:Using Custom Fields | Custom Fields]] data.? The Custom Fields should have a key of 'tag' and a value of 'email' | |||
* You have created a [[WordPress:Pages|Page]] and associated a [[WordPress:Pages#Page_Templates|Page Template]] with that page.? For this example, assume the Template Name is 'Qbased' and was copied from the ''wp-content/themes/index.php'' template.? If you are not familiar with this process, follow the instructions in [[WordPress:Pages#Creating_your_own_Page_Templates|Creating your own Page Templates]]. | |||
* As this is a somewhat advanced developer topic, familiarity with the core WordPress concept of [[WordPress:The Loop]] is suggested. | |||
== Code for the Page Template == | |||
===The query=== | |||
To begin with, it is necessary to retrieve the [[WordPress:Glossary#Recordset|recordset]] containing the posts you want to display.? To do this, create a result set using the WordPress [[WordPress:Function_Reference/wpdb_Class|$wpdb database class]].? Note that the [[WordPress:Glossary#MySQL|MySQL]] [[WordPress:Wikipedia:SELECT|SELECT]] statement illustrates a '''simple''' [[WordPress:Wikipedia:JOIN|JOIN]].? Here, <tt>$pageposts</tt> will contain an [[WordPress:Glossary#Array|array]] of objects. Each object will represent a '''published''' post that has custom field key-value pair - with the key being 'tag' and the value being 'email': | |||
<pre> | <pre> | ||
? <?php | ? <?php | ||
第42行: | 第35行: | ||
</pre> | </pre> | ||
===The Revised Loop=== | |||
Now, to display posts collected into <tt>$pageposts</tt> by the previous [[WordPress:Wikipedia:SELECT|SELECT]] criteria, you need to replace [[WordPress:The Loop]] with your own loop code in the ''Qbased'' Page Template.? This requires creating a revised loop that cycles through the posts stored in <tt>$pageposts</tt> and displays them. Note: the structure / markup in the loop below is taken from the WordPress '''default''' [[WordPress:Using Themes|theme]]. | |||
<pre> | <pre> | ||
? <?php if ($pageposts): ?> | ? <?php if ($pageposts): ?> | ||
第69行: | 第61行: | ||
</pre> | </pre> | ||
And that's it! | |||
? | |||
To go through the important parts of the code, line by line, you have: | |||
* A test to make sure that the query that populated <tt>$pageposts</tt> actually found some posts that matched the [[WordPress:Wikipedia:SELECT|SELECT]] criteria: | |||
<pre> | <pre> | ||
<?php if ($pageposts): ?> | <?php if ($pageposts): ?> | ||
</pre> | </pre> | ||
*[[WordPress:Wikipedia:Foreach|foreach loop]] | * A [[WordPress:Wikipedia:Foreach|foreach loop]] to go through the posts returned in <tt>$pageposts</tt> and display them: | ||
<pre> | <pre> | ||
<?php foreach($pageposts as $post): ?> | <?php foreach($pageposts as $post): ?> | ||
</pre> | </pre> | ||
* | * And, a call to the WordPress post formatting function, <tt>setup_postdata()</tt>, that automatically populates the required variables: | ||
<pre> | <pre> | ||
<?php setup_postdata($post); ?> | <?php setup_postdata($post); ?> | ||
</pre> | </pre> | ||
==== Loop | ==== Within the Loop ==== | ||
Because <tt>setup_postdata($post);</tt> was called in our example, you can use the same [[WordPress:Template Tags | template tags]] that can be included in a normal WordPress post loop, like <tt>the_content()</tt> and <tt>the_permalink()</tt>. This means that you can create your own post display results using a Page Template with a minimum amount of fuss, automatically taking advantage of the various plugins you may have activated in your WordPress blog to provide extra formatting and functionality. | |||
=== | === The Completed Page Template === | ||
Here is a complete example of the new template that works with the WordPress '''default''' theme. | |||
<pre> | <pre> | ||
<?php | <?php | ||
第151行: | 第141行: | ||
<?php get_footer(); ?> | <?php get_footer(); ?> | ||
</pre> | </pre> | ||
? | It is important to note here that the above example will work '''only''' when OBJECT is passed as the "output_type" parameter for <code>$wpdb->get_results()</code>. setup_postdata() does not seem to work when ARRAY_A or ARRAY_N is passed in $wpdb->get_results(). | ||
==Query based on Custom Field and Category== | |||
== | This next example sets the $querystr variable used in the above example, to get all posts in Categories 1,2, and 3, that have the meta_key 'paragraf', and then sorted ascending by the meta_values.? This example gleaned from Otto42's response in [http://wordpress.org/support/topic/121011 Forum Topic 121011]. | ||
<pre> | <pre> | ||
? $querystr = " | ? $querystr = " | ||
第172行: | 第160行: | ||
'''with wordpress 2.3 you need to update the sql query shown above to this:''' | |||
This example gleaned from kernow's response in [http://wordpress.org/support/topic/121011 Forum Topic 121011] | |||
<pre> | <pre> | ||
第187行: | 第175行: | ||
ORDER BY $wpdb->postmeta.meta_value ASC | ORDER BY $wpdb->postmeta.meta_value ASC | ||
</pre> | </pre> | ||
== | ? | ||
== Acknowledgements == | |||
? | |||
Many thanks to [http://wordpress.org/support/profile/6445 Kafkaesquii] for pointing out a simpler method of populating the appropriate global variables, etc, using setup_postdata(). |