當評論存儲在WordPress數據庫中時,評論所涉及的帖子(或頁面)的ID也被存儲。
麻煩的是,你試圖用WordPress保存評論,但對於一個頁面,它實際上並不知道知道左右。
那麼,怎麼樣我們創建了一個的WordPress頁每個真正頁,而僅僅是作爲一個表示,讓你真正的網頁和WordPress有一個共同點相互合作。
因此,這裏的計劃是;
- 將WordPress加載到每個「真實」頁面的背景中。
- 看看一個WordPress頁面表示已經存在的「真實」頁面
- 如果它沒有,創建它,然後有
- WordPress的伎倆思成我們實際上觀察表示
- 進關於使用所有WP的函數和'模板標籤',就像你通常使用的那樣
這段代碼應該在模板文件開始的某個地方用來渲染你的'真實'頁面;
include ('../path/to/wp-load.php');
// remove query string from request
$request = preg_replace('#\?.*$#', '', $_SERVER['REQUEST_URI']);
// try and get the page name from the URI
preg_match('#podpages/([a-z0-9_-]+)#', $matches);
if ($matches && isset($matches[1])) {
$pagename = $matches[1];
// try and find the WP representation page
$query = new WP_Query(array('pagename' => $pagename));
if (!$query->have_posts()) {
// no WP page exists yet, so create one
$id = wp_insert_post(array(
'post_title' => $pagename,
'post_type' => 'page',
'post_status' => 'publish',
'post_name' => $pagename
));
if (!$id)
do_something(); // something went wrong
}
// this sets up the main WordPress query
// from now on, WordPress thinks you're viewing the representation page
}
UPDATE
我簡直不敢相信我是這個愚蠢的。下面應該替換外面的當前代碼if
;
// try and find the WP representation page - post_type IS required
$query = new WP_Query(array('name' => $pagename, 'post_type' => 'page'));
if (!$query->have_posts()) {
// no WP page exists yet, so create one
$id = wp_insert_post(array(
'post_title' => $pagename,
'post_type' => 'page',
'post_status' => 'publish',
'post_name' => $pagename,
'post_author' => 1, // failsafe
'post_content' => 'wp_insert_post needs content to complete'
));
}
// this sets up the main WordPress query
// from now on, WordPress thinks you're viewing the representation page
// post_type is a must!
wp(array('name' => $pagename, 'post_type' => 'page'));
// set up post
the_post();
P.S我認爲使用query_var name
在pagename
更適合 - 它查詢蛞蝓,而不是塞「路徑」。
您還需要要麼將與名稱redirect_to
形式和URL的值裏面輸入你想重定向到,或,過濾器掛在comment_post_redirect
功能重定向,返回正確的網址。
據我所知,這是不能做到的。它與Wordpress將數據存儲在數據庫中的方式有關。你正在使用錯誤的工具來完成這項工作。嘗試使用Disqus或Echo。 – mattbasta 2010-05-26 17:25:18
沒有機會。我在這個博客中的評論系統被修改,所以我必須使用wordpress的正常評論系統 – choise 2010-05-31 07:00:58
我沒有看到任何理由堅持評論的wordpress系統,如果這些是在博客的單獨頁面上的評論。如果你想對用戶的WP數據庫進行認證,這是完全不同的問題,可以輕鬆完成。您可以自己訂購新的評論系統或程序。 – 2010-05-31 11:11:04