欢迎转载!转载时请注明出处:http://blog.csdn.net/nfer_zhuang/article/details/51318917
引言
由于项目中大部分页面都是通过模板创建的,因此我需要统计一下所有页面各自的模板文件是什么。
另外,在知乎有这样的一个问题: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/