WordPress中查看所有頁面信息

歡迎轉載!轉載時請註明出處: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.

– 摘自WordPress Support

而通過函數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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章