久久精品水蜜桃av综合天堂,久久精品丝袜高跟鞋,精品国产肉丝袜久久,国产一区二区三区色噜噜,黑人video粗暴亚裔
站長百科 | 數(shù)字化技能提升教程 數(shù)字化時代生存寶典
首頁
數(shù)字化百科
電子書
建站程序
開發(fā)
服務器
辦公軟件
開發(fā)教程
服務器教程
軟件使用教程
運營教程
熱門電子書
WordPress教程
寶塔面板教程
CSS教程
Shopify教程
導航
程序頻道
推廣頻道
網(wǎng)賺頻道
人物頻道
網(wǎng)站程序
網(wǎng)頁制作
云計算
服務器
CMS
論壇
網(wǎng)店
虛擬主機
cPanel
網(wǎng)址導航
WIKI使用導航
WIKI首頁
最新資訊
網(wǎng)站程序
站長人物
頁面分類
使用幫助
編輯測試
創(chuàng)建條目
網(wǎng)站地圖
站長百科導航
站長百科
主機偵探
IDCtalk云說
跨境電商導航
WordPress啦
站長專題
網(wǎng)站推廣
網(wǎng)站程序
網(wǎng)站賺錢
虛擬主機
cPanel
網(wǎng)址導航專題
云計算
微博營銷
虛擬主機管理系統(tǒng)
開放平臺
WIKI程序與應用
美國十大主機
編輯“
WordPress:Displaying Posts Using a Custom Select Query
”
人物百科
|
營銷百科
|
網(wǎng)賺百科
|
站長工具
|
網(wǎng)站程序
|
域名主機
|
互聯(lián)網(wǎng)公司
|
分類索引
Xxf3325
(
討論
|
貢獻
)
2008年7月24日 (四) 18:03的版本
(新頁面: == Description == At some point in your WordPress development career you may be presented with the need to display one or more posts using
SELECT
criteria n...)
(差異) ←上一版本 |
最后版本
(
差異
) |
下一版本→
(
差異
)
跳轉至:
導航
、?
搜索
警告:您正在編輯的是本頁面的舊版本。
如果您發(fā)布該更改,該版本后的所有更改都會丟失。
警告:
您沒有登錄。如果您做出任意編輯,您的IP地址將會公開可見。如果您
登錄
或
創(chuàng)建
一個賬戶,您的編輯將歸屬于您的用戶名,且將享受其他好處。
反垃圾檢查。
不要
加入這個!
== 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> <?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> ===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> <?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> 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> <?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> * 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> ==== 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> <?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> 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> $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> '''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> 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().
摘要:
請注意,您對站長百科的所有貢獻都可能被其他貢獻者編輯,修改或刪除。如果您不希望您的文字被任意修改和再散布,請不要提交。
您同時也要向我們保證您所提交的內(nèi)容是您自己所作,或得自一個不受版權保護或相似自由的來源(參閱
Wordpress-mediawiki:版權
的細節(jié))。
未經(jīng)許可,請勿提交受版權保護的作品!
取消
編輯幫助
(在新窗口中打開)
取自“
http://kktzf.com.cn/wiki/WordPress:Displaying_Posts_Using_a_Custom_Select_Query
”