1. 程式人生 > >WordPress中檢視所有頁面資訊

WordPress中檢視所有頁面資訊

引言

由於專案中大部分頁面都是通過模板建立的,因此我需要統計一下所有頁面各自的模板檔案是什麼。
另外,在知乎有這樣的一個問題:wordpress的頁面ID在哪裡檢視?,我的回答是:能用程式碼解決的問題,絕不手動去一個個的檢視。

程式碼

直接上程式碼:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>檢視頁面列表</title>
    <style
>
table {border: 1px solid #cdcdcd;text-align: left;} td, th {padding-right: 10px;} </style> </head> <body> <div id="page-content"> <?php require( dirname(__FILE__) . '/wp-load.php' ); $pages = get_pages(); echo
"<table >"; echo "<tr>"; echo "<th>index</th>"; echo "<th>ID</th>"; echo "<th>permalink</th>"; // echo "<th>post_type</th>"; // echo "<th>guid</th>"; echo
"<th>post_title</th>"; echo "<th>post_name</th>"; // echo "<th>post_status</th>"; echo "<th>post_status</th>"; echo "</tr>"; $index = 0; foreach ($pages as $page) { echo "<tr>"; echo "<td>".$index++."</td>"; echo "<td>".$page->ID."</td>"; echo "<td>".get_permalink($page)."</td>"; // echo "<td>".$page->post_type."</td>"; // echo "<td>".$page->guid."</td>"; echo "<td>".$page->post_title."</td>"; echo "<td>".$page->post_name."</td>"; // echo "<td>".$page->post_status."</td>"; echo "<td>".get_page_template_slug($page)."</td>"; echo "</tr>"; } echo "</table>"; ?>
</div> </body> </html>

程式碼比較簡單,就不詳細解釋了。
其中post_title和post_name需要單獨解釋一下:

my post_name says one thing and my post_title says something else. Why are they different and how to I make them the same?

post_title is the post’s actual title. post_name is that post’s unique part of the permalink - sometimes called the slug.

而通過函式get_page_template_slug($page)獲取的就是模板檔案,注意函式get_page_template_slug($page)返回的是模板檔名:

Return: (string|false) Page template filename. Returns an empty string when the default page template is in use. Returns false if the post is not a page.

那麼,如何獲取模板名呢?
$template = get_post_meta( $id, '_wp_page_template', true );
– 摘自http://themeforest.net/