我正在嘗試讓 React 應用程序呈現為 Wordpress 頁面的一部分。我使用create-react-app并添加了我的應用程序"homepage": "." 到package.json構建之前。首先,我通過將必要的核心文件嵌入到標準 html 文檔中來嘗試該功能。<!DOCTYPE html><html><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title></head><body> <div id="wp-root"></div><script src="runtime-main.363857b3.js"></script><script src="main.ef284662.chunk.js"></script><script src="2.319ef205.chunk.js"></script></body></html>果然它起作用了!當接近 Wordpress 時,我將腳本文件放入functions.php.wp_enqueue_script( 'script', get_template_directory_uri() . '/js/test.js', true);wp_enqueue_script( 'script', get_template_directory_uri() . '/js/2.319ef205.chunk.js', true);wp_enqueue_script( 'script', get_template_directory_uri() . '/js/main.ef284662.chunk.js', true);wp_enqueue_script( 'script', get_template_directory_uri() . '/js/runtime-main.363857b3.js', true);我還在alert("test")同一文件夾中查詢了一個簡單的測試腳本,以確保它能夠響應。我的測試腳本按預期在每個頁面上觸發,因此我繼續將我的測試腳本包含<div id="wp-root"> 在頁面上,并將科學包含在header.phpbody 標簽下我的測試腳本有效,我的 React 構建在測試中有效index.html。為什么 WordPress 不渲染它?
如何在 WordPress 頁面上呈現 React?
largeQ
2023-09-28 15:23:22