搜索
SR網頁設計工作室 - 購物網站/企業官網專業開發 Wordpress 使用教程 Wordpress 模板上客製化自定義文章輸出 (依類別、型態.. ...
byadmin 發表於 2014-7-15 23:22:01 , 2722人已閱讀 , 0人回應
Wordpress 目前已經被越來越大量應用在內容管理系統的用途上,而非僅是拿來作為blog使用,因此在客製化網站時,常常會有需要在頁面上顯示各類別的文章列表清單 !  當然如果用某些國外開發模板,會很貼心內建一些視覺化工具模組,方便你直接拉區塊,製作頁面,但若你是開發者絕對會有很多無法滿足需求的狀況出現,因此學會如何使用wp函數來調用個是文章資訊是非常重要的一件事。

WordPress在輸出文章調用的時候提供了兩個很好用的功能函數 query_posts()和WP_Query()可以很方便的控制文章輸出。如果在模板上調用文章時有衝突則改用另一種函數調用,通常可以直接避開很多難解的衝突問題。

query_posts()範例用法:
  1. <?php
  2. $args = array(
  3.     'orderby'   => 'date',
  4.     'order'   => 'ASC',
  5.     'paged' => $paged
  6. );
  7. query_posts( $args );
  8. while ( have_posts() ) : the_post();
  9.     echo '<li>';
  10.     the_title();
  11.     echo '</li>';
  12. endwhile;
  13. wp_reset_query();
  14. ?>
複製代碼
WP_Query()範例用法:
  1. <?php
  2. $args = array(
  3.     'orderby'   => 'date',
  4.     'order'   => 'ASC',
  5.     'paged' => $paged
  6. );
  7. $the_query = new WP_Query( $args );
  8. if ( $the_query->have_posts() ) {
  9.         echo '<ul>';
  10.         while ( $the_query->have_posts() ) {
  11.                 $the_query->the_post();
  12.                 echo '<li>' . get_the_title() . '</li>';
  13.         }
  14.         echo '</ul>';
  15. }
  16. wp_reset_postdata();
  17. ?>
複製代碼
其實看到上面範例可以發現,兩種調用寫法其實很相似,都是先使用參數陣列做一些文章調用的判斷,之後在呼叫輸出,包含 html 標籤。

$args參數說明:

作者參數
author :(整數) 作者的ID,author=-3 表示排除的作者ID為 3 posts;
author_name:(字符) 作者的nicename(NOT name),author_name=phplog


分類參數
cat (整數): 分類的ID
category_name (字符): 分類的slug(NOT name)
category__and(數組): 分類的ID
category__in (數組): 分類的ID
category__not_in (數組): 分類的ID


標籤參數
tag (字符): 標籤的ID
tag__and (整數): 標籤的ID
tag__in (數組): 標籤的ID
tag__not_in(數組): 標籤的ID
tag_slug__and(數組): 標籤的slug(NOT name)
tag_slug__and(數組): 標籤的slug(NOT name)


處理多個分類
{tax}(分類):(字符) 分類的名字
tax_query:(數組) 分類的名字
taxonomy(字符): 分類名稱
field(字符): id或者slug
terms(字符): 跟上面成對出現field'field' => 『slug',則』terms' => array( 『action', 『commedy' );』field' => 『id',,則』terms' => array( 103, 115, 206 )。
include_children(布爾): 是否包含子類
operator (字符):IN', 『NOT IN', 『AND'
Post & Page 參數
p (整數):post ID
name (字符):post slug
page_id (整數):page ID
page_nmae (字符):page slug
post_parent (整數):page id,只返回當前子類pages。
post__in (數組):page id
post__not___in (數組):page id
post_type類型參數:表示文章類型,值包括post、page、revision、attachment、any
post_status:表示文章狀態,值包括pending(待審)、publish(已發佈)、auto-draft(草稿), future(定時), private(私有), trash(已刪除)。


頁碼/排序參數
showposts (整數):顯示文章篇數
posts_per_page (整數):每頁文章顯示篇數
posts_per_archive_page (整數):作用在search和archive
nopaging (布爾):true時則不分頁顯示,直接顯示全部文章
paged (整數):表示跳到第幾頁
order(字符):』ASC'(順序)』DESC'(倒序)
orderby (字符):按照哪些只來排序,值有 『ID'(文章id)、』author'(文章作者)、』title'(文章標題)、』date'(文章作者)、』modified'(文章修改時間)、』parent'(文章父級分類)、』rand'(隨機)、』comment_count'(最新評論時間)、』meta_value'(隨機)、』meta_value'(自定單位名稱,用法:』orderby' => 『meta_value', 『meta_key' => 『price' )。$args = array(
    // 2, 6就是你不想显示的分类ID,多个用半角逗号隔开
    'category__not_in'   => array(2, 6),
    'paged' => 1
);
query_posts($args);


時間參數
year (整數):年
monthnum (整數):月份
w (整數):星期
day (整數):天
hour (整數):小時
minute (整數):分
second (整數):秒

可以發現,只要配合上面的參數去撰寫文章調用判斷,幾乎可以百分之百客製化各式各樣的文章列表 !  當然CSS要靠自己去雕刻就是了 ...

以下展示一些使用範例 (以下範例只有函數判斷,輸出模板請自行撰寫)

調用 7 月 15 日的文章:
  1. query_posts('monthnum=7&day=15');
複製代碼
讓某分類的文章不顯示/顯示:
  1. $args = array(
  2.     // 2, 6為不顯示的分類ID,使用逗號間隔
  3.     'category__not_in'   => array(2, 6),
  4.     'paged' => 1
  5. );
  6. query_posts($args);
複製代碼
調用分類名稱(slug)為products的文章,並顯示10篇:
  1. $args = array(
  2.     'category_name'=> 'products'
  3.     'showposts' => 10,
  4.     'paged' => 1
  5. );
  6. query_posts($args);
複製代碼
調用自訂形態文章 portfolio,並顯示10篇 (國外模板幾乎必帶portfolio文章型態):

  1. $args = array(
  2.     'post_type' => 'portfolio'   
  3.     'showposts' => 10,
  4.     'paged' => 1
  5. );
  6. query_posts($args);
複製代碼
Traffic Exchange Site
您需要登錄後才可以回帖 登錄 | 立即註冊

作者資訊

文章分類

SR數位設計工作室

  • 服務專線:03-3555-069


ECSHOP購物網站開發|接案說明|線上洽詢|隱私權政策|SR數位設計(CMS)

信箱:sr.design2011@gmail.com

, Processed in 0.080809 second(s), 40 queries , Gzip On.

Copyright © 2011-2014 Template By SR網頁設計清新部落風格

Core - DZ2.5 GMT+8, 2024-3-29 02:04

回頂部