久久精品水蜜桃av综合天堂,久久精品丝袜高跟鞋,精品国产肉丝袜久久,国产一区二区三区色噜噜,黑人video粗暴亚裔
站長百科 | 數字化技能提升教程 數字化時代生存寶典
首頁
數字化百科
電子書
建站程序
開發(fā)
服務器
辦公軟件
開發(fā)教程
服務器教程
軟件使用教程
運營教程
熱門電子書
WordPress教程
寶塔面板教程
CSS教程
Shopify教程
導航
程序頻道
推廣頻道
網賺頻道
人物頻道
網站程序
網頁制作
云計算
服務器
CMS
論壇
網店
虛擬主機
cPanel
網址導航
WIKI使用導航
WIKI首頁
最新資訊
網站程序
站長人物
頁面分類
使用幫助
編輯測試
創(chuàng)建條目
網站地圖
站長百科導航
站長百科
主機偵探
IDCtalk云說
跨境電商導航
WordPress啦
站長專題
網站推廣
網站程序
網站賺錢
虛擬主機
cPanel
網址導航專題
云計算
微博營銷
虛擬主機管理系統(tǒng)
開放平臺
WIKI程序與應用
美國十大主機
編輯“
WordPress:Displaying Posts Using a Custom Select Query
”
人物百科
|
營銷百科
|
網賺百科
|
站長工具
|
網站程序
|
域名主機
|
互聯(lián)網公司
|
分類索引
Xxf3325
(
討論
|
貢獻
)
2008年10月17日 (五) 15:41的版本
(
→?已修改的Loop(循環(huán))
)
(
差異
)
←上一版本
|
最后版本
(
差異
) |
下一版本→
(
差異
)
跳轉至:
導航
、?
搜索
警告:您正在編輯的是本頁面的舊版本。
如果您發(fā)布該更改,該版本后的所有更改都會丟失。
警告:
您沒有登錄。如果您做出任意編輯,您的IP地址將會公開可見。如果您
登錄
或
創(chuàng)建
一個賬戶,您的編輯將歸屬于您的用戶名,且將享受其他好處。
反垃圾檢查。
不要
加入這個!
== 描述 == 在你的WordPress發(fā)展歷程的某個時刻,或許你需要通過非WordPress的[[WordPress:Template Tags/query posts | query_posts]] 架構提供的[[WordPress:Wikipedia:SELECT|選擇]]標準來顯示一篇或更多的文章。例如,你有時或許必須[[WordPress:Wikipedia:JOIN|加入]] WordPress表格以確定要顯示的文章或使用自己表格中的儲存數據來確定要顯示[[WordPress:Writing Posts|正在寫的文章]]。 以下概述的實例展示了這樣一個過程:選擇有[[WordPress:Using Custom Fields | 定制區(qū)域]]存入值的所有文章并把它們在建立在[[WordPress:Pages#Creating_your_own_Page_Templates|頁面模板]]基礎上的[[WordPress:Pages|頁面]]顯示出來。這些編碼最初用來執(zhí)行文章標簽[[WordPress:Plugins|插件]],它允許以比WordPress[[WordPress:Manage_Categories_SubPanel|分類]]稍無序的構架來組織文章。你的使用或許有所不同,但以下的例子仍會給你提供些有用的一般過程說明。 === 文章假設 === 本文通常假設你擁有[[WordPress:Glossary#PHP|PHP]], [[WordPress:Glossary#MySQL|MySQL]]知識和WordPress 使用能力。 但此例的特定假設是: *你至少擁有一篇[[WordPress:Using Custom Fields |定制區(qū)域]]數據文章。自定義區(qū)域有“標簽”鍵和“電子郵件”鍵值。 *你已創(chuàng)建一個[[WordPress:Pages|頁面]]并有一個[[WordPress:Pages#Page_Templates|頁面模板]]鏈接。在此例中,假定模板名稱為'Qbased',它是從wp-content/themes/index.php模板復制的。如果你對此過程不熟悉,按照[[WordPress:Pages#Creating_your_own_Page_Templates|創(chuàng)建你自己的頁面模板]]中的說明進行操作。 *由于這是稍先進的開發(fā)主題,推薦掌握[[WordPress:The Loop|循環(huán)]]的WordPress核心概念。 == 網頁模板編碼 == ===查詢=== 首先,需要檢索[[WordPress:Glossary#Recordset|數據集],里面包含你要顯示的文章。要做到這一點,需要使用WordPress[[WordPress:Function_Reference/wpdb_Class|$wpdb 數據庫 類]]創(chuàng)建一個(結果集)。注意[[WordPress:Glossary#MySQL|MySQL]] [[WordPress:Wikipedia:SELECT|SELECT]]指令闡明了一個“簡單的” [[WordPress:Wikipedia:JOIN|JOIN]]。<tt>$pageposts</tt>在此會包含[[WordPress:Glossary#Array|數組]]對象。每個對象代表一篇有自定義字段key-value(鍵-鍵值)配對且鍵名為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> ===已修改的Loop(循環(huán))=== 現在,若要通過以前的[[WordPress:Wikipedia:SELECT|SELECT]] 標準來顯示<tt>$pageposts</tt>中的文章,你需要用Qbased網頁模板中你自己的循環(huán)編碼來替代[[WordPress:The Loop]]。這需要創(chuàng)建一個已修改的循環(huán)(loop),使它能夠循環(huán)<tt>$pageposts</tt>中的文章并顯示它們。注意:下面loop(循環(huán))中的結構/標記取自WordPress“默認”[[WordPress:Using Themes|主題]]. 。 <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> <?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> 就是它! 一行一行地審查編碼的重要部分,你必須: *測試以確保<tt>$pageposts</tt>中的查詢可以查到符合[[WordPress:Wikipedia:SELECT|SELECT]]標準的文章: <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> <?php if ($pageposts): ?> </pre> *[[WordPress:Wikipedia:Foreach|foreach loop]]審查已返回<tt>$pageposts</tt>的文章,并顯示文章: <pre> <?php foreach($pageposts as $post): ?> </pre> *調用WordPress文章格式化函數,<tt>setup_postdata()</tt>,自動填入所需變量: <pre> <?php setup_postdata($post); ?> </pre> ==== Loop (循環(huán))內部==== 由于例子中調用了<tt>setup_postdata($post);</tt>,你可以使用可包括在正常Wordpress循環(huán)(loop)中的相同[[WordPress:Template Tags |模板標簽]],如<tt>the_content()</tt> 和 <tt>the_permalink()</tt>。這意味著你能夠較方便地用網頁模板創(chuàng)建自己的文章顯示結果,并自動利用你的Wordpress博客已激活的各種插件提供額外的格式和功能。 ===已完成的網頁模板=== 這是Wordpress“默認”主題運行的新模板的完整事例。 <pre> <?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(); ?> </pre> 這里值得注意的是,上述例子“只有”當<code>$wpdb->get_results()</code>是以"output_type"為參數,面向對象通過時才運行。當ARRAY_A 或ARRAY_N在$wpdb->get_results()通過時,setup_postdata()似乎不運行。 ==定制區(qū)域和類別基礎上的查詢== 此事例設置了上個事例中使用的$querystr變量以獲得類別1,2,和3中的有meta_key 'paragraf'的所有文章,并按meta_values升序排列。此例來源于Otto42在[http://wordpress.org/support/topic/121011 Forum Topic 121011]. 中的回復。 <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> '''使用 wordpress2.3,你需要把以上顯示的sql查詢更新為:''' 此例來源于kernow在[http://wordpress.org/support/topic/121011 Forum Topic 121011] 中的回復 <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> ==致謝 == 非常感謝[http://wordpress.org/support/profile/6445 Kafkaesquii]指出填寫適當全局變量等的更簡便方法:使用setup_postdata()。
摘要:
請注意,您對站長百科的所有貢獻都可能被其他貢獻者編輯,修改或刪除。如果您不希望您的文字被任意修改和再散布,請不要提交。
您同時也要向我們保證您所提交的內容是您自己所作,或得自一個不受版權保護或相似自由的來源(參閱
Wordpress-mediawiki:版權
的細節(jié))。
未經許可,請勿提交受版權保護的作品!
取消
編輯幫助
(在新窗口中打開)
取自“
http://kktzf.com.cn/wiki/WordPress:Displaying_Posts_Using_a_Custom_Select_Query
”