【问题标题】:Rewrite URL in wordpress child theme在 wordpress 子主题中重写 URL
【发布时间】:2013-04-12 08:56:42
【问题描述】:

我正在制作一个 wordpress 子主题作为应用程序。我想重写主题中的 URL,因为它有时对 seo 更友好。

例如,如果有人进入 www.domain.com/show-new-cars/

我想加载服务器 www.domain.com/index.php?loadcars=1

我试图将它放在子主题文件夹functions.php 中,但没有任何结果。

 add_action( 'init', 'wpse7379_init' );
function wpse7379_init()
{
add_rewrite_rule('show-new-cars/',
'index.php?loadcars=1',
'top');
}

【问题讨论】:

  • 你刷新你的重写规则了吗?尝试保存永久链接结构以使它们刷新。
  • 是的,这似乎解决了它。谢谢!

标签: wordpress url-rewriting


【解决方案1】:

$wp_rewrite->flush_rules();成功了。

这是一个更新,因为不是每次页面加载时都使用刷新,因为它会增加服务器负载分配。

//When activated trough admin
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {

    add_action( 'init', 'wpse7379_init' );
    function wpse7379_init()
    {
    add_rewrite_rule('show-new-cars/',
    'index.php?loadcars=1',
    'top');
    }
    $wp_rewrite->flush_rules();

}

【讨论】:

  • 这种方法的一个特点是,每次页面加载时刷新规则都会增加开销。标准方法是在插件安装程序回调中刷新规则,或者在从主题添加路由时检查规则是否存在。
  • 添加代码,使其仅在通过管理员激活主题时运行,感谢反馈
猜你喜欢
  • 2014-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-16
  • 2016-03-04
  • 2019-05-13
  • 1970-01-01
相关资源
最近更新 更多