我正在制作一个wordpress插件,它实际上是一个Angular 6 javascript应用程序。我有以下代码,让它在wordpress中作为短代码工作
function msp_helloworld_load(){
ob_start();
wp_enqueue_script( 'my_custom_script', plugin_dir_url( __FILE__ ) . 'app/runtime.js' );
wp_enqueue_script( 'my_custom_script1', plugin_dir_url( __FILE__ ) . 'app/polyfills.js' );
wp_enqueue_script( 'my_custom_script2', plugin_dir_url( __FILE__ ) . 'app/styles.js' );
wp_enqueue_script( 'my_custom_script3', plugin_dir_url( __FILE__ ) . 'app/vendor.js' );
wp_enqueue_script( 'my_custom_script4', plugin_dir_url( __FILE__ ) . 'app/main.js' );
echo '<base href="' . $_SERVER[ 'REQUEST_URI' ] . '">';
echo '<app-root></app-root>';
echo ob_get_clean();
}
add_shortcode( 'helloworld', 'msp_helloworld_load' );
在这个应用程序中,我使用了一个简单的Angular 6路由器,如下所示
const appRoutes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'contact', component: ContactComponent }
];
我将这个简短的代码嵌入到wordpress的示例页面中,如下所示
[helloworld]
这是渲染良好,路由器正在工作,而我在它点击它。这两个urls如下所示
http://localhost/wp/sample-page
http://localhost/wp/sample-page/contact
当我重新加载页面时出现问题。它找不到任何东西。
我的.htaccess看起来像这样
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /hn/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /hn/index.php [N]
RewriteRule ^ /sample-page/$1 [L]
</IfModule>
# END WordPress
但它仍然不起作用。我不擅长.htaccess,所以请帮帮忙。
发布于 2018-06-10 22:08:10
太好了,我已经算出来了。这是一个解决方案,首先你不必触摸.htaccess,因为它是默认的wordpress安装
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /hn/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /hn/index.php [L]
</IfModule>
# END WordPress
然后将wordpress固定链接更改为postname
然后这里是诀窍,在基础标签上点你想要嵌入短代码的页面。您可以接受来自用户的参数以指向内容条目中的基本url,或者您甚至可以使其动态放置页面url。就这样。
function msp_helloworld_load(){
ob_start();
wp_enqueue_script( 'my_custom_script', plugin_dir_url( __FILE__ ) . 'app/runtime.js' );
wp_enqueue_script( 'my_custom_script1', plugin_dir_url( __FILE__ ) . 'app/polyfills.js' );
wp_enqueue_script( 'my_custom_script2', plugin_dir_url( __FILE__ ) . 'app/styles.js' );
wp_enqueue_script( 'my_custom_script3', plugin_dir_url( __FILE__ ) . 'app/vendor.js' );
wp_enqueue_script( 'my_custom_script4', plugin_dir_url( __FILE__ ) . 'app/main.js' );
echo '<base href="http://localhost/wp/sample-page/">';
echo '<app-root></app-root>';
echo ob_get_clean();
}
add_shortcode( 'helloworld', 'msp_helloworld_load' );
还要添加自定义url重写。
function myplugin_rewrite_tag_rule() {
add_rewrite_rule( '^sample-page/([^/]*)/?', 'sample-page?city=$matches[1]','top' );
}
add_action('init', 'myplugin_rewrite_tag_rule', 10, 0);
https://stackoverflow.com/questions/50779784
复制相似问题