我使用Wordpress作為無頭CMS。
我使用下面的函式來回傳一個基于頁面標題的頁面。
沒有改變任何代碼或Wordpress的內容,突然它停止了作業......
在不同網站的同一服務器上,幾乎相同的代碼都能正常作業。
有誰知道可能發生了什么?
有誰知道?
// -- Get page based on page name --/
function getWordpressPage($wordpressPageName) /span>{
$page = NULL。
$pageRequest = cleanUrlVar($wordpressPageName)。
if($pageRequest){
$page = get_page_by_title( $pageRequest ) 。
if($page != NULL){
setup_postdata($page)。
return the_content()。
}
}
if($page == ''/span>){
header('Location: /')。
exit()。
}
FYI - var_dump($page)回傳資料,所以Wordpress回傳了一些東西,看起來它在'return the_content()'階段倒下了...
uj5u.com熱心網友回復:
the_content()將通過echo顯示內容。所以如果你想獲得
變數來回傳,你應該使用get_the_content()
更新你的代碼以回傳它:
return get_the_content()。
你可以在這里找到更多的資訊:
https://developer.wordpress.org/reference/functions/get_the_content/
uj5u.com熱心網友回復:
你甚至不需要setup_postdata。通過使用get_page_by_title,你將得到整個page物件。
get_page_by_titleDocs。
function getWordpressPage($wordpressPageName)
{
$page = NULL;
$pageRequest = cleanUrlVar($wordpressPageName)。
if ($pageRequest) {
$page = get_page_by_title($pageRequest, ARRAY_A)。
if ($page != NULL) {
return $page['post_content'] 。
}
}
if ($page == '') {
header('Location: /')。
exit()。
}
注意:
cleanUrlVar是一個你正在使用的自定義函式,所以要確保它能作業。- 我在
get_page_by_title中使用了ARRAY_A,以獲得array格式的結果 。
可能的替代方法,使用setup_postdata
setup_postdata將接受post object或post id而不是page title。所以你可以使用post全域變數并將其傳遞給setup_postdata。雖然這樣做也可以,但是,可能你根本不需要這樣做。
。
function getWordpressPage($wordpressPageName=''/span>){
global $post;
setup_postdata($post)。
$content = get_the_content()。
return $content;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/331798.html
標籤:
