很多 WordPress 博客都想取得所有文章的永久連結(permalinks), 用來作一些網上推廣用途, 但尋遍 WordPress 管理系統後台都沒有這功能。 小弟今天就和大家分享幾個月前在互聯網上找到的一個簡單 PHP 小程式, 可以很方便的列出 WordPress 所有文章的永久連結 (網上也有博客將 permalinks 翻譯為固定網地址)。

以下便是可以列出 WordPress 所有文章永久連結的 PHP 程式碼:

<?php

include "wp-load.php";

/*
Define post_status as required:

post_staus = publish
    return posts and pages with type, URL and title

post_status= any 
    return in addition any attachments such as images
    in that post immediately after in the list 
    with URL and file name  */

$posts = new WP_Query('post_type=any&posts_per_page=-1&post_status=publish');

$posts = $posts->posts;

header('Content-type:text/plain');

foreach($posts as $post) {
    switch ($post->post_type) {
        case 'revision':
        case 'nav_menu_item':
            break;
        case 'page':
            $permalink = get_page_link($post->ID);
            break;
        case 'post':
            $permalink = get_permalink($post->ID);
            break;
        case 'attachment':
            $permalink = get_attachment_link($post->ID);
            break;
        default:
            $permalink = get_post_permalink($post->ID);
            break;
    }
    // print out post type, permalink and post title
    //echo "\n{$post->post_type}\t{$permalink}\t{$post->post_title}";

    // print out permalink only
    echo $permalink . "\n";
}
?>

現在看看如何使用以上的PHP 程式碼, 列出 WordPress 所有文章的永久連結:

1. 打開文字編輯軟件 (Text Editor) , 並開啟一新檔案。

2. 將以上的PHP 程式碼 Copy 在文字編輯軟件的新檔案。

3. 將檔案儲存為 url-listing.php PHP檔案。

4. 然後上載往 WordPress 網頁寄存的 Home 目錄, 如下圖所示:

5. 接著開啟瀏覽器, 前往 url-listing.php 的網址, 例如:

http://www.YourBlog.com/url-listing.php

所有 WordPress 文章 (Post) 和分頁 (Page) 都會在瀏覽器列印出來:

多站網路網誌 (Multisite Network) 怎麼辦?

如果是 WordPress 多站網路網誌 (Multisite Network), 就按每個網誌的網址續個的去爬便可以了。

就舉 3Tic 多站網路網誌為例, 現在有三個網誌:

列印第一個網誌所有文章的永久連結的網址是:

http://www.3tic.com/stockinvest/url-listing.php

列印第二個網誌所有文章的永久連結的網址是:

http://www.3tic.com/financialplan/url-listing.php

列印第三個網誌所有文章的永久連結的網址是:

http://www.3tic.com/makemoney/url-listing.php

取得 WordPress 所有文章的永久連結(permalinks), 就是這麼簡單!