|
? |
(未顯示2個(gè)用戶的11個(gè)中間版本) |
第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.
| |
| == 描述 == | | == 描述 == |
| | 在你的WordPress發(fā)展歷程的某個(gè)時(shí)刻,或許你需要通過非WordPress的[[WordPress:Template Tags/query posts | query_posts]] 架構(gòu)提供的[[WordPress:Wikipedia:SELECT|選擇]]標(biāo)準(zhǔn)來顯示一篇或更多的文章。例如,你有時(shí)或許必須[[WordPress:Wikipedia:JOIN|加入]] WordPress表格以確定要顯示的文章或使用自己表格中的儲(chǔ)存數(shù)據(jù)來確定要顯示[[WordPress:Writing Posts|正在寫的文章]]。 |
|
| |
|
| 在你的WordPress發(fā)展歷程的某個(gè)時(shí)刻,或許你需要通過非WordPress的' [[WordPress:Template Tags/query posts | query_posts]] 架構(gòu)提供的[[WordPress:Wikipedia:SELECT|SELECT]]標(biāo)準(zhǔn)來顯示一篇或更多的文章。例如,你有時(shí)或許必須[[WordPress:Wikipedia:JOIN|JOIN]] WordPress表格以確定要顯示的文章或使用自己表格中的儲(chǔ)存數(shù)據(jù)來確定要顯示的[[WordPress:Writing Posts|posts]]。
| | 以下概述的實(shí)例展示了這樣一個(gè)過程:選擇有[[WordPress:Using Custom Fields | 定制區(qū)域]]存入值的所有文章并把它們在建立在[[WordPress:Pages#Creating_your_own_Page_Templates|頁面模板]]基礎(chǔ)上的[[WordPress:Pages|頁面]]顯示出來。這些編碼最初用來執(zhí)行文章標(biāo)簽[[WordPress:Plugins|插件]],它允許以比WordPress[[WordPress:Manage_Categories_SubPanel|分類]]稍無序的構(gòu)架來組織文章。你的使用或許有所不同,但以下的例子仍會(huì)給你提供些有用的一般過程說明。 |
| 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.
| |
| ? | |
| 以下概述的實(shí)例展示了選擇有[[WordPress:Using Custom Fields | Custom Field]]存入值的所有文章并把它們在建立在[[WordPress:Pages#Creating_your_own_Page_Templates|Page Template]]基礎(chǔ)上的[[WordPress:Pages|Page]]顯示出來的過程。這些編碼最初用來執(zhí)行文章標(biāo)簽[[WordPress:Plugins|plugin]],它允許以比WordPress[[WordPress:Manage_Categories_SubPanel|Categories]]稍無序的構(gòu)架來組織文章。你的使用或許有所不同,但以下的例子仍會(huì)給你提供些有用的一般過程說明。
| |
| ? | |
| === 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.
| |
|
| |
|
| === 文章假設(shè) === | | === 文章假設(shè) === |
|
| |
|
| 本文通常假設(shè)你擁有[[WordPress:Glossary#PHP|PHP]], [[WordPress:Glossary#MySQL|MySQL]]知識(shí)和WordPress 使用能力。 | | 本文通常假設(shè)你擁有[[WordPress:Glossary#PHP|PHP]], [[WordPress:Glossary#MySQL|MySQL]]知識(shí)和WordPress 使用能力。 |
|
| |
| Specific assumptions for the example, however, are:
| |
|
| |
|
| 但此例的特定假設(shè)是: | | 但此例的特定假設(shè)是: |
| * 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'
| | *你至少擁有一篇[[WordPress:Using Custom Fields |定制區(qū)域]]數(shù)據(jù)文章。自定義區(qū)域有“標(biāo)簽”鍵和“電子郵件”鍵值。 |
| *你至少擁有一篇[[WordPress:Using Custom Fields | Custom Fields]]數(shù)據(jù)文章。自定義字段有“標(biāo)簽”鍵和“電子郵件”鍵值。
| |
|
| |
|
| * 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]].
| | *你已創(chuàng)建一個(gè)[[WordPress:Pages|頁面]]并有一個(gè)[[WordPress:Pages#Page_Templates|頁面模板]]鏈接。在此例中,假定模板名稱為'Qbased',它是從wp-content/themes/index.php模板復(fù)制的。如果你對(duì)此過程不熟悉,按照[[WordPress:Pages#Creating_your_own_Page_Templates|創(chuàng)建你自己的頁面模板]]中的說明進(jìn)行操作。 |
| *你已創(chuàng)建一個(gè)[[WordPress:Pages|Page]]并有一個(gè)[[WordPress:Pages#Page_Templates|Page Template]]鏈接。在此例中,假定模板名稱為'Qbased',它是從wp-content/themes/index.php模板復(fù)制的。如果你對(duì)此過程不熟悉,按照[[WordPress:Pages#Creating_your_own_Page_Templates|Creating your own Page Templates]]中的說明進(jìn)行操作。 | |
|
| |
|
| * As this is a somewhat advanced developer topic, familiarity with the core WordPress concept of [[WordPress:The Loop]] is suggested.
| | *由于這是稍先進(jìn)的開發(fā)主題,推薦掌握[[WordPress:The Loop|循環(huán)]]的WordPress核心概念。 |
| *由于這是稍先進(jìn)的開發(fā)主題,推薦掌握[[WordPress:The Loop]]的核心WordPress概念。 | |
|
| |
|
| == Code for the Page Template ==
| |
| == 網(wǎng)頁模板編碼 == | | == 網(wǎng)頁模板編碼 == |
|
| |
|
| ===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':
| |
|
| |
|
| 首先,需要檢索包含你要顯示文章的[[WordPress:Glossary#Recordset|recordset]](數(shù)據(jù)集)。要做到這一點(diǎn),需要使用WordPress[[WordPress:Function_Reference/wpdb_Class|$wpdb database class]]創(chuàng)建一個(gè)result set(結(jié)果集)。注意[[WordPress:Glossary#MySQL|MySQL]] [[WordPress:Wikipedia:SELECT|SELECT]]指令闡明了一個(gè)“簡單的” [[WordPress:Wikipedia:JOIN|JOIN]]。<tt>$pageposts</tt>在此會(huì)包含[[WordPress:Glossary#Array|array]]對(duì)象。每個(gè)對(duì)象代表一篇有自定義字段key-value(鍵-鍵值)配對(duì)且鍵名為tag,值為email的“已發(fā)布”文章。
| | 首先,需要檢索[[WordPress:Glossary#Recordset|數(shù)據(jù)集],里面包含你要顯示的文章。要做到這一點(diǎn),需要使用WordPress[[WordPress:Function_Reference/wpdb_Class|$wpdb 數(shù)據(jù)庫 類]]創(chuàng)建一個(gè)(結(jié)果集)。注意[[WordPress:Glossary#MySQL|MySQL]] [[WordPress:Wikipedia:SELECT|SELECT]]指令闡明了一個(gè)“簡單的” [[WordPress:Wikipedia:JOIN|JOIN]]。<tt>$pageposts</tt>在此會(huì)包含[[WordPress:Glossary#Array|數(shù)組]]對(duì)象。每個(gè)對(duì)象代表一篇有自定義字段key-value(鍵-鍵值)配對(duì)且鍵名為tag,值為email的“已發(fā)布”文章。 |
| ? | |
| <pre>
| |
| <?php
| |
|
| |
|
| $querystr = "
| |
| ? ? SELECT wposts.*
| |
| ? ? FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
| |
| ? ? WHERE wposts.ID = wpostmeta.post_id
| |
| ? ? AND wpostmeta.meta_key = 'tag'
| |
| ? ? AND wpostmeta.meta_value = 'email'
| |
| ? ? AND wposts.post_status = 'publish'
| |
| ? ? AND wposts.post_type = 'post'
| |
| ? ? ORDER BY wposts.post_date DESC
| |
| ";
| |
|
| |
|
| $pageposts = $wpdb->get_results($querystr, OBJECT);
| |
|
| |
| ?>
| |
| </pre>
| |
| <pre> | | <pre> |
| ? <?php | | ? <?php |
第75行: |
第43行: |
|
| |
|
|
| |
|
| ===The Revised Loop===
| |
| ===已修改的Loop(循環(huán))=== | | ===已修改的Loop(循環(huán))=== |
| ? | | 現(xiàn)在,若要通過以前的[[WordPress:Wikipedia:SELECT|SELECT]] 標(biāo)準(zhǔn)來顯示<tt>$pageposts</tt>中的文章,你需要用Qbased網(wǎng)頁模板中你自己的循環(huán)編碼來替代[[WordPress:The Loop]]。這需要?jiǎng)?chuàng)建一個(gè)已修改的循環(huán)(loop),使它能夠循環(huán)<tt>$pageposts</tt>中的文章并顯示它們。注意:下面loop(循環(huán))中的結(jié)構(gòu)/標(biāo)記取自WordPress“默認(rèn)”[[WordPress:Using Themes|主題]]. |
| 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]].
| |
| ? | |
| 現(xiàn)在,若要通過以前的[[WordPress:Wikipedia:SELECT|SELECT]] 標(biāo)準(zhǔn)來顯示<tt>$pageposts</tt>中的文章,你需要用Qbased網(wǎng)頁模板中你自己的循環(huán)編碼來替代[[WordPress:The Loop]]。這需要?jiǎng)?chuàng)建一個(gè)已修改的循環(huán)(loop),使它能夠循環(huán)<tt>$pageposts</tt>中的文章并顯示它們。注意:下面loop(循環(huán))中的結(jié)構(gòu)/標(biāo)記取自WordPress“默認(rèn)”[[WordPress:Using Themes|theme]]. | |
| 。 | | 。 |
|
| |
| <pre>
| |
| <?php if ($pageposts): ?>
| |
| <?php foreach ($pageposts as $post): ?>
| |
| <?php setup_postdata($post); ?>
| |
|
| |
| <div class="post" id="post-<?php the_ID(); ?>">
| |
| <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
| |
| ? ? <?php the_title(); ?></a></h2>
| |
| ? ? <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
| |
| ? ? <div class="entry">
| |
| ? ? ? <?php the_content('Read the rest of this entry »'); ?>
| |
| ? ? </div>
| |
| ? ? <p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>?
| |
| ? ? <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
| |
| </div>
| |
| <?php endforeach; ?>
| |
| <?php else : ?>
| |
| ? ? <h2 class="center">Not Found</h2>
| |
| ? ? <p class="center">Sorry, but you are looking for something that isn't here.</p>
| |
| ? ? <?php include (TEMPLATEPATH . "/searchform.php"); ?>
| |
| <?php endif; ?>
| |
| </pre>
| |
| <pre> | | <pre> |
| ? <?php if ($pageposts): ?> | | ? <?php if ($pageposts): ?> |
第128行: |
第69行: |
| </pre> | | </pre> |
|
| |
|
|
| |
| And that's it!
| |
| 就是它! | | 就是它! |
|
| |
|
| To go through the important parts of the code, line by line, you have:
| | 一行一行地審查編碼的重要部分,你必須: |
|
| |
|
| 一行一行地審查編碼的重要部分,你必須:
| | *測試以確保<tt>$pageposts</tt>中的查詢可以查到符合[[WordPress:Wikipedia:SELECT|SELECT]]標(biāo)準(zhǔn)的文章: |
|
| |
|
| * 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:
| |
| *測試以確保<tt>$pageposts</tt>中的查詢可以查到符合[[WordPress:Wikipedia:SELECT|SELECT]]標(biāo)準(zhǔn)中的文章:
| |
|
| |
| <pre>
| |
| <?php if ($pageposts): ?>
| |
| </pre>
| |
| * A [[WordPress:Wikipedia:Foreach|foreach loop]] to go through the posts returned in <tt>$pageposts</tt> and display them:
| |
| <pre>
| |
| <?php foreach($pageposts as $post): ?>
| |
| </pre>
| |
| <pre> | | <pre> |
| <?php if ($pageposts): ?> | | <?php if ($pageposts): ?> |
| </pre> | | </pre> |
| * A [[WordPress:Wikipedia:Foreach|foreach loop]] to go through the posts returned in <tt>$pageposts</tt> and display them: | | *[[WordPress:Wikipedia:Foreach|foreach loop]]審查已返回<tt>$pageposts</tt>的文章,并顯示文章: |
| <pre> | | <pre> |
| <?php foreach($pageposts as $post): ?> | | <?php foreach($pageposts as $post): ?> |
| </pre> | | </pre> |
|
| |
|
| ? | | *調(diào)用WordPress文章格式化函數(shù),<tt>setup_postdata()</tt>,自動(dòng)填入所需變量: |
| * And, a call to the WordPress post formatting function, <tt>setup_postdata()</tt>, that automatically populates the required variables:
| |
| <pre>
| |
| <?php setup_postdata($post); ?>
| |
| </pre>
| |
| *調(diào)用WordPress文章格式化函數(shù),<tt>setup_postdata()</tt>,自動(dòng)填入所需變量:
| |
| <pre> | | <pre> |
| <?php setup_postdata($post); ?> | | <?php setup_postdata($post); ?> |
第165行: |
第89行: |
| ? | | ? |
|
| |
|
| ==== Within the Loop ====
| |
| ==== Loop (循環(huán))內(nèi)部==== | | ==== Loop (循環(huán))內(nèi)部==== |
| | 由于例子中調(diào)用了<tt>setup_postdata($post);</tt>,你可以使用可包括在正常Wordpress循環(huán)(loop)中的相同[[WordPress:Template Tags |模板標(biāo)簽]],如<tt>the_content()</tt> 和 <tt>the_permalink()</tt>。這意味著你能夠較方便地用網(wǎng)頁模板創(chuàng)建自己的文章顯示結(jié)果,并自動(dòng)利用你的Wordpress博客已激活的各種插件提供額外的格式和功能。 |
|
| |
|
|
| |
| 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.
| |
| 由于例子中調(diào)用了<tt>setup_postdata($post);</tt>,你可以使用可包括在正常Wordpress循環(huán)(loop)中的相同[[WordPress:Template Tags | template tags]],如<tt>the_content()</tt> 和 <tt>the_permalink()</tt>。這意味著你可以最小的麻煩用網(wǎng)頁模板創(chuàng)建自己的文章顯示結(jié)果,并自動(dòng)利用你的Wordpress博客已激活的各種插件提供額外的格式和功能。
| |
|
| |
| === The Completed Page Template ===
| |
| ===已完成的網(wǎng)頁模板=== | | ===已完成的網(wǎng)頁模板=== |
|
| |
|
| |
| Here is a complete example of the new template that works with the WordPress '''default''' theme.
| |
| 這是Wordpress“默認(rèn)”主題運(yùn)行的新模板的完整事例。 | | 這是Wordpress“默認(rèn)”主題運(yùn)行的新模板的完整事例。 |
|
| |
| <pre> | | <pre> |
| <?php | | <?php |
第237行: |
第152行: |
| </pre> | | </pre> |
|
| |
|
| <pre>
| | 這里值得注意的是,上述例子“只有”當(dāng)<code>$wpdb->get_results()</code>是以"output_type"為參數(shù),面向?qū)ο笸ㄟ^時(shí)才運(yùn)行。當(dāng)ARRAY_A 或ARRAY_N在$wpdb->get_results()通過時(shí),setup_postdata()似乎不運(yùn)行。 |
| <?php
| | ==定制區(qū)域和類別基礎(chǔ)上的查詢== |
| /*
| | 此事例設(shè)置了上個(gè)事例中使用的$querystr變量以獲得類別1,2,和3中的有meta_key 'paragraf'的所有文章,并按meta_values升序排列。此例來源于Otto42在[http://wordpress.org/support/topic/121011 Forum Topic 121011]. |
| Template Name: Qbased
| |
| */
| |
| ?>
| |
| ? | |
| <?php get_header(); ?>
| |
| ? | |
| <div id="content" class="narrowcolumn">
| |
| ? | |
| <?php
| |
| ? | |
| $querystr = "
| |
| ? ? SELECT wposts.*
| |
| ? ? FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
| |
| ? ? WHERE wposts.ID = wpostmeta.post_id
| |
| ? ? AND wpostmeta.meta_key = 'tag'
| |
| ? ? AND wpostmeta.meta_value = 'email'
| |
| ? ? AND wposts.post_status = 'publish'
| |
| ? ? AND wposts.post_type = 'post'
| |
| ? ? AND wposts.post_date < NOW()
| |
| ? ? ORDER BY wposts.post_date DESC
| |
| ";
| |
| ? | |
| $pageposts = $wpdb->get_results($querystr, OBJECT);
| |
| ? | |
| ?>
| |
| <?php if ($pageposts): ?>
| |
| ? <?php foreach ($pageposts as $post): ?>
| |
| ? ? <?php setup_postdata($post); ?>
| |
| ? | |
| ? ? <div class="post" id="post-<?php the_ID(); ?>">
| |
| ? ? ? <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
| |
| ? ? ? <?php the_title(); ?></a></h2>
| |
| ? ? ? <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
| |
| ? ? ? <div class="entry">
| |
| ? ? ? ? <?php the_content('Read the rest of this entry ?'); ?>
| |
| ? ? ? </div>
| |
| ?
| |
| ? ? ? <p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>?
| |
| ? ? ? <?php comments_popup_link('No Comments ?', '1 Comment ?', '% Comments ?'); ?></p>
| |
| ? ? </div>
| |
| ? <?php endforeach; ?>
| |
| ?
| |
| ? <?php else : ?>
| |
| ? ? <h2 class="center">Not Found</h2>
| |
| ? ? <p class="center">Sorry, but you are looking for something that isn't here.</p>
| |
| ? ? <?php include (TEMPLATEPATH . "/searchform.php"); ?>
| |
| <?php endif; ?>
| |
| ? | |
| </div>
| |
| ? | |
| <?php get_sidebar(); ?>
| |
| ? | |
| <?php get_footer(); ?>
| |
| </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().
| |
| 這里值得注意的是上述例子“只有”在對(duì)象通過的是<code>$wpdb->get_results()</code>的"output_type"參數(shù)時(shí)才運(yùn)行。當(dāng)ARRAY_A 或ARRAY_N在$wpdb->get_results()通過時(shí),setup_postdata()似乎不運(yùn)行。
| |
| ? | |
| ? | |
| ==Query based on Custom Field and Category== | |
| ? | |
| ==自定義字段和類別基礎(chǔ)上的查詢==
| |
| ? | |
| 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].
| |
| 下個(gè)事例設(shè)置了上個(gè)事例中使用的$querystr變量以獲得類別1,2,和3中的有meta_key 'paragraf'的所有文章,并按meta_values升序排列。此例來源于Otto42在[http://wordpress.org/support/topic/121011 Forum Topic 121011].
| |
| 中的回復(fù)。 | | 中的回復(fù)。 |
|
| |
| <pre>
| |
| $querystr = "
| |
| ? ? SELECT $wpdb->posts.*
| |
| ? ? FROM $wpdb->posts
| |
| ? ? LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
| |
| ? ? LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id)
| |
| ? ? WHERE $wpdb->postmeta.meta_key = 'paragraf'
| |
| ? ? AND $wpdb->posts.post_status = 'publish'
| |
| ? ? AND $wpdb->posts.post_type = 'post'
| |
| ? ? AND $wpdb->post2cat.category_id IN (1,2,3)
| |
| ? ? ORDER BY $wpdb->postmeta.meta_value ASC
| |
| ? ? ";
| |
|
| |
| </pre>
| |
|
| |
| <pre> | | <pre> |
| ? $querystr = " | | ? $querystr = " |
第337行: |
第171行: |
| </pre> | | </pre> |
|
| |
|
|
| |
| '''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]
| |
|
| |
|
| '''使用 wordpress2.3,你需要把以上顯示的sql查詢更新為:''' | | '''使用 wordpress2.3,你需要把以上顯示的sql查詢更新為:''' |
| 此例來源于kernow在[http://wordpress.org/support/topic/121011 Forum Topic 121011] 中的回復(fù) | | 此例來源于kernow在[http://wordpress.org/support/topic/121011 Forum Topic 121011] 中的回復(fù) |
| | |
|
| |
|
| <pre> | | <pre> |
第355行: |
第187行: |
| ORDER BY $wpdb->postmeta.meta_value ASC | | ORDER BY $wpdb->postmeta.meta_value ASC |
| </pre> | | </pre> |
| <pre>
| |
| SELECT * FROM $wpdb->posts
| |
| LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
| |
| LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
| |
| LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
| |
| WHERE $wpdb->term_taxonomy.term_id = 1,2,3
| |
| AND $wpdb->term_taxonomy.taxonomy = 'category'
| |
| AND $wpdb->posts.post_status = 'publish'
| |
| AND $wpdb->postmeta.meta_key = 'paragraf'
| |
| ORDER BY $wpdb->postmeta.meta_value ASC
| |
| </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().
| |
| 非常感謝[http://wordpress.org/support/profile/6445 Kafkaesquii]指出填寫適當(dāng)全局變量等的更簡便方法:使用setup_postdata()。 | | 非常感謝[http://wordpress.org/support/profile/6445 Kafkaesquii]指出填寫適當(dāng)全局變量等的更簡便方法:使用setup_postdata()。 |
描述[ ]
在你的WordPress發(fā)展歷程的某個(gè)時(shí)刻,或許你需要通過非WordPress的 query_posts 架構(gòu)提供的選擇標(biāo)準(zhǔn)來顯示一篇或更多的文章。例如,你有時(shí)或許必須加入 WordPress表格以確定要顯示的文章或使用自己表格中的儲(chǔ)存數(shù)據(jù)來確定要顯示正在寫的文章。
以下概述的實(shí)例展示了這樣一個(gè)過程:選擇有 定制區(qū)域存入值的所有文章并把它們在建立在頁面模板基礎(chǔ)上的頁面顯示出來。這些編碼最初用來執(zhí)行文章標(biāo)簽插件,它允許以比WordPress分類稍無序的構(gòu)架來組織文章。你的使用或許有所不同,但以下的例子仍會(huì)給你提供些有用的一般過程說明。
文章假設(shè)[ ]
本文通常假設(shè)你擁有PHP, MySQL知識(shí)和WordPress 使用能力。
但此例的特定假設(shè)是:
- 你至少擁有一篇定制區(qū)域數(shù)據(jù)文章。自定義區(qū)域有“標(biāo)簽”鍵和“電子郵件”鍵值。
- 你已創(chuàng)建一個(gè)頁面并有一個(gè)頁面模板鏈接。在此例中,假定模板名稱為'Qbased',它是從wp-content/themes/index.php模板復(fù)制的。如果你對(duì)此過程不熟悉,按照創(chuàng)建你自己的頁面模板中的說明進(jìn)行操作。
- 由于這是稍先進(jìn)的開發(fā)主題,推薦掌握循環(huán)的WordPress核心概念。
網(wǎng)頁模板編碼[ ]
查詢[ ]
首先,需要檢索[[WordPress:Glossary#Recordset|數(shù)據(jù)集],里面包含你要顯示的文章。要做到這一點(diǎn),需要使用WordPress$wpdb 數(shù)據(jù)庫 類創(chuàng)建一個(gè)(結(jié)果集)。注意MySQL SELECT指令闡明了一個(gè)“簡單的” JOIN。$pageposts在此會(huì)包含數(shù)組對(duì)象。每個(gè)對(duì)象代表一篇有自定義字段key-value(鍵-鍵值)配對(duì)且鍵名為tag,值為email的“已發(fā)布”文章。
<?php
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'tag'
AND wpostmeta.meta_value = 'email'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
已修改的Loop(循環(huán))[ ]
現(xiàn)在,若要通過以前的SELECT 標(biāo)準(zhǔn)來顯示$pageposts中的文章,你需要用Qbased網(wǎng)頁模板中你自己的循環(huán)編碼來替代WordPress:The Loop。這需要?jiǎng)?chuàng)建一個(gè)已修改的循環(huán)(loop),使它能夠循環(huán)$pageposts中的文章并顯示它們。注意:下面loop(循環(huán))中的結(jié)構(gòu)/標(biāo)記取自WordPress“默認(rèn)”主題.
。
<?php if ($pageposts): ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php endforeach; ?>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
就是它!
一行一行地審查編碼的重要部分,你必須:
- 測試以確保$pageposts中的查詢可以查到符合SELECT標(biāo)準(zhǔn)的文章:
<?php if ($pageposts): ?>
<?php foreach($pageposts as $post): ?>
- 調(diào)用WordPress文章格式化函數(shù),setup_postdata(),自動(dòng)填入所需變量:
<?php setup_postdata($post); ?>
Loop (循環(huán))內(nèi)部[ ]
由于例子中調(diào)用了setup_postdata($post);,你可以使用可包括在正常Wordpress循環(huán)(loop)中的相同模板標(biāo)簽,如the_content() 和 the_permalink()。這意味著你能夠較方便地用網(wǎng)頁模板創(chuàng)建自己的文章顯示結(jié)果,并自動(dòng)利用你的Wordpress博客已激活的各種插件提供額外的格式和功能。
已完成的網(wǎng)頁模板[ ]
這是Wordpress“默認(rèn)”主題運(yùn)行的新模板的完整事例。
<?php
/*
Template Name: Qbased
*/
?>
<?php get_header(); ?>
<div id="content" class="narrowcolumn">
<?php
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'tag'
AND wpostmeta.meta_value = 'email'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
AND wposts.post_date < NOW()
ORDER BY wposts.post_date DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
<?php if ($pageposts): ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content('Read the rest of this entry ?'); ?>
</div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>
<?php comments_popup_link('No Comments ?', '1 Comment ?', '% Comments ?'); ?></p>
</div>
<?php endforeach; ?>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
這里值得注意的是,上述例子“只有”當(dāng)$wpdb->get_results()
是以"output_type"為參數(shù),面向?qū)ο笸ㄟ^時(shí)才運(yùn)行。當(dāng)ARRAY_A 或ARRAY_N在$wpdb->get_results()通過時(shí),setup_postdata()似乎不運(yùn)行。
定制區(qū)域和類別基礎(chǔ)上的查詢[ ]
此事例設(shè)置了上個(gè)事例中使用的$querystr變量以獲得類別1,2,和3中的有meta_key 'paragraf'的所有文章,并按meta_values升序排列。此例來源于Otto42在Forum Topic 121011.
中的回復(fù)。
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id)
WHERE $wpdb->postmeta.meta_key = 'paragraf'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->post2cat.category_id IN (1,2,3)
ORDER BY $wpdb->postmeta.meta_value ASC
";
使用 wordpress2.3,你需要把以上顯示的sql查詢更新為:
此例來源于kernow在Forum Topic 121011 中的回復(fù)
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.term_id = 1,2,3
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->postmeta.meta_key = 'paragraf'
ORDER BY $wpdb->postmeta.meta_value ASC
致謝[ ]
非常感謝Kafkaesquii指出填寫適當(dāng)全局變量等的更簡便方法:使用setup_postdata()。