在 WordPress 客製化中,其中一個最常用到的功能就是取得某一個分類中的特定文章(如:前5篇)。今天在這一篇教學中,就是要來透過php程式,來取得特定分類的某幾篇文章,然後取出該篇文章的一些基本資訊。
就讓我們開始吧~
本教學主要會使用到的 WP 函式:get_posts()。詳細說明可以參考官網連結:https://developer.wordpress.org/reference/functions/get_posts/
做法主要是透過帶入函式的變數,去指定分類的類別、排序方法、取出數量。函式會return出一個資料陣列,裡面就是每一個post的資料了。可以指定的變數以及預設值如下:
$defaults = array(
‘numberposts’ => 5, //要取出的數量,預設5篇
‘category’ => 0, //分類的sn
‘orderby’ => ‘date’, //排序依據,預設為日期
‘order’ => ‘DESC’, //排序方式
‘include’ => array(), //要包含的指定項目
‘exclude’ => array(), //要排除的項目
‘meta_key’ => ”, //指定的meta鍵
‘meta_value’ => ”, //meta的值
‘post_type’ => ‘post’, //種類,是post 或是 page
‘suppress_filters’ => true, //是否要用過濾器
);
如果沒有特別需要的設定,就保留預定即可。
而實際的操作會如下面:
取出指定分類中的第一篇文章做法:
$args = array(
'posts_per_page' => 1, // 只取一篇文章
'cat' => '3', // 指定分類id為3
);
//其他沒有指定,則表示,依照日期降冪排列取出,因此,這樣取出來的就是最新的那一篇文章。
$posts = get_posts($args);
$post = $posts[0];
echo "標題:" . $post->post_title;
echo "文章編號:" . $post->ID;
如果是要取出該分類中的前10篇文章,除了在posts_per_page中指定數量為10之外,也會使用 foreach () 迴圈取出各篇文章,完整作法如下:
$args = array(
'posts_per_page' => 10, // 這邊我們調整為10篇
'cat' => '3', // 指定分類id
);
$posts = get_posts($args);
$resultStr = '';
//以下以表格的方式呈現內容
$resultStr .= "<div style='overflow-x:auto;'>
<table>
<thead>
<tr>
<th data-hide='phone' style='text-align:center;'><h4 class='home_news_table_title'>發佈時間</h4></th>
<th data-class='expand' style='width:70%;text-align:center;'><h4 class='home_news_table_title'>文章標題與內容</h4></th>
<th data-hide='phone' style='text-align:center;' class='mobile_disappear'><h4 class='home_news_table_title'>文章分類</h4></th>
</tr>
</thead>
<tbody>";
//使用foreach迴圈列出所有內容
foreach ($posts as $post) {
$read_more_link = "<a href='" . get_permalink($post->ID) . "' class='read_more_bottom'>Read More</a>";
$categories = get_the_category($post->ID); //取得該篇文章的所屬分類,有可能會有不只一個分類出現
$cat_amount = count($categories);
$ii = 1;
$catStr = '';
foreach ($categories as $category) {
$catStr .= "<a href='" . get_category_link($category->term_id) . "' alt='View all posts in" . $category->name . "'>" . $category->name . "</a>";
if ($ii < $cat_amount) $catStr .= "<br>";
$ii++;
}
$postDate = explode(" ", $post->post_modified);
$resultStr .= " <tr>
<td class='home_news_date'>" . $postDate['0'] . "</td>
<td><h3 class='home_news_title'>
<a class='home_news_title_link' href='" . get_permalink($post->ID) . "'>" . apply_filters('the_title', $post->post_title) . "</a></h3>
<p class='home_news_excerpt'>" . mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 70, '', 'UTF-8') . "... " . $read_more_link . "</p>
</td>
<td class='home_news_cate mobile_disappear' style='text-align:center;'>" . $catStr . "</td>
</tr>
";
}
$resultStr .= "</tbody></table></div>";
echo $resultStr;
程式說明:
上面的範例中,多了一個函式 get_the_category(),這是帶入文章ID,取得這一篇的所屬分類。因為有可能會不只一個分類,所以再使用一個foreach迴圈來列出所有分類。而可以得到的訊息有:分類ID($category->term_id)、分類名稱($category->name)。
另外,也透過 get_category_link() 這個WP內建函示,取得分類連結。要取得文章連結,則用 get_permalink() 這個WP內建函示。
以上就是今天的教學,希望你會喜歡。
參考網站連結:
- get_posts() 函式說明https://developer.wordpress.org/reference/functions/get_posts/
- Display the latest post from a category in a page https://wordpress.stackexchange.com/questions/180576/display-the-latest-post-from-a-category-in-a-page
- get_the_category()函式說明 https://developer.wordpress.org/reference/functions/get_the_category/
- https://stackoverflow.com/questions/17303840/get-category-name-from-post-id
- get_permalink()函式說明 https://developer.wordpress.org/reference/functions/get_permalink/
- get_post() 函式說明 https://developer.wordpress.org/reference/functions/get_post/
附錄:
post物件中包含的方法如下:
post_author
post_date
post_date_gmt
post_content
post_title
post_excerpt
post_status
comment_status
ping_status
post_password
post_name
to_ping
pinged
post_modified
post_modified_gmt
post_content_filtered
post_parent
guid
menu_order
post_type
post_mime_type
comment_count
filter
Recent Comments