問題描述
我想為我的 WordPress 部落格建立一個自定義頁面,該頁面將執行我的 PHP 程式碼,同時保留了整個網站 CSS /主題/設計的一部分。
PHP 程式碼將使用第三方 API(所以我需要包括其他 PHP 檔案)
我該如何做到這一點?
注:我沒有特定的需要與 Wordpress API 互動 – 除了包括某些其他 PHP 庫我需要我沒有其他的依賴關係的 PHP 程式碼,我想包括在一個 WP 頁面。顯然,任何不需要學習 WP API 的解決方案都將是最好的解決方案。
最佳解決方案
您不需要與 API 進行互動或使用外掛。
首先,在您的主題資料夾 (/wp-content/themes/themename/) 下複製 post.php 或 page.php 。
將新檔案重新命名為 templatename.php(其中,模板名稱是您想要呼叫的新模板) 。要將新模板新增到可用模板列表中,請在新檔案的頂部輸入以下內容:
<?php
/*
Template Name: Name of Template
*/
?>
您可以修改此檔案 (使用 PHP) 包含其他檔案或任何您需要的。
然後在您的 WordPress 部落格中建立一個新頁面,在頁面編輯螢幕中,您將在右側的 「屬性」 小工具中看到一個 「模板」 下拉式清單。選擇您的新模板併發布頁面。
您的新頁面將使用 templatename.php 中定義的 PHP 程式碼
次佳解決方案
如果你像我一樣,有時你希望能夠在 CMS 中不存在的頁面中引用 WordPress 的功能。這樣,它仍然是後端具體的,不能被客戶意外地刪除。
透過使用 php require()包含 wp-blog-header.php 檔案,這實際上很簡單。
這是一個使用查詢字串為任何帖子生成 Facebook OG 資料的示例。
以 http://example.com/yourfilename.php?1 為例,其中 1 是我們要生成 OG 資料的帖子的 ID 的示例:
現在在 yourfilename.php 的內容中,為了方便起見,它位於根 WP 目錄中:
<?php
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
$uri = $_SERVER['REQUEST_URI'];
$pieces = explode("?", $uri);
$post_id = intval( $pieces[1] );
// og:title
$title = get_the_title($post_id);
// og:description
$post = get_post($post_id);
$descr = $post->post_excerpt;
// og:image
$img_data_array = get_attached_media('image', $post_id);
$img_src = null;
$img_count = 0;
foreach ( $img_data_array as $img_data ) {
if ( $img_count > 0 ) {
break;
} else {
++$img_count;
$img_src = $img_data->guid;
}
} // end og:image
?>
<!DOCTYPE HTML>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=yes" />
<meta property="og:title" content="<?php echo $title; ?>" />
<meta property="og:description" content="<?php echo $descr; ?>" />
<meta property="og:locale" content="en_US" />
<meta property="og:type" content="website" />
<meta property="og:url" content="<?php echo site_url().'/your_redirect_path'.$post_id; ?>" />
<meta property="og:image" content="<?php echo $img_src; ?>" />
<meta property="og:site_name" content="Your Title" />
</html>
在那裡你有它:生成共享模型的任何帖子使用帖子的實際影像,摘錄和標題!
我們可以建立一個特殊的模板並編輯永久連結結構來執行此操作,但是由於它只需要一個頁面,因為我們不希望客戶端從 CMS 中刪除它,這似乎是一個更清潔的選項。
第三種解決方案
建立模板頁面是正確的答案,只需將其新增到您在主題資料夾中建立的頁面中即可
<?php
/*
Template Name: mytemplate
*/
?>
對於執行此程式碼,您需要從後端選擇 mytemplate 作為頁面的模板
請檢視此連結以獲取正確的詳細資訊 https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/。
第四種方案
如果您想建立自己的.php 檔案,並與 Wordpress 無 404 標頭互動,並保持您當前的永久連結結構,則不需要該頁面的模板檔案,我發現這種方法在.php 檔案中效果最好:
<?php
require_once(dirname(__FILE__) . '/wp-config.php');
$wp->init();
$wp->parse_request();
$wp->query_posts();
$wp->register_globals();
$wp->send_headers();
// Your WordPress Functions here...
echo site_url();
?>
之後,您可以簡單地執行任何 wordpress 功能。此外,這假設您的.php 檔案位於您的 wp-config.php 檔案所在的 wordpress 站點的根目錄中。
對我來說,這是一個 PRICELESS 發現,因為我正在使用 require_once(dirname(__FILE__) . '/wp-blog-header.php'); 最長的時間,因為 Wordpress 甚至告訴你,這應該用於整合 Wordpress 函式的方法,cept,它導致 404 標題,這是奇怪的,他們會想要使用這種方法。 https://codex.wordpress.org/Integrating_WordPress_with_Your_Website
我知道很多人已經回答了這個問題,它已經有了一個可以接受的答案,但是這是一個很好的方法,在你的 wordpress 網站根目錄下的一個.php 檔案 (或技術上你想要在你的網站上),你可以瀏覽並載入無 404 標題!
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。