【问题标题】:Wordpress - URL directory parameter without redirectWordpress - 没有重定向的 URL 目录参数
【发布时间】:2018-04-02 08:51:09
【问题描述】:

我创建了一个 wordpress 插件,它使用短代码和 URL 参数来加载特定文本。

默认页面是:

   domainname.com/test

   [shortcode for plugin]

... default template ...

它可以使用这些参数加载一些文本:

domainname.com/test?book_id=BWBR0002656&art_id=1

       [shortcode for plugin]

... loaded the text for the book_id and art_id ...

我想将 javascript 参数 url 从当前的 domainname.com/test?book_id=BWBR0002656&art_id=1 更改为 domainname.com/test/BWBR0002656/1 不会触发 wordpress 404 重定向。

这可能与短代码页面仍在运行有关吗?

【问题讨论】:

  • 短代码有什么作用?您可以发布短代码的代码吗? JavaScript 与 URL 有什么关系?您是否使用 book_id 或 art_id 的 URL slugs 注册了任何自定义帖子类型?
  • 短代码实际与 URL 无关,它只是加载 javascript 文件。 javascript 从插件目录加载某本书和文章(它们只是带有内容的 html 文件,不涉及 php)。我知道如何将 ?book_id 的 javascript 工作更改为 //
    ,但我被困在 404 重定向部分。它必须与 .htaccess 一起使用?
  • 您可以手动更改 .htaccess,但 WordPress 的方式是使用 Rewrite API。如果你不这样做,你将冒着在 WP 安装中破坏许多其他东西的风险。更好的办法是编写一个具有自定义帖子类型的插件,该插件使用/test/ slug 和文章的重写端点。这也可以通过重写查询来完成,但这肯定需要更多的工作。您可能会通过一些创造性的方式使用Advanced Custom Post Fields
  • 我宁愿编写代码,然后创造性地使用一些可能无法满足我所有需求的插件
  • 我给你写个大纲……你想让/test成为一个单页还是会有更多类似/test这样的“东西”?

标签: wordpress plugins parameters shortcode wordpress-shortcode


【解决方案1】:

我设法让它使用这个:

add_action( 'init', 'wpa5413_init' );
function wpa5413_init()
{
    // Remember to flush the rules once manually after you added this code!
    add_rewrite_rule(
        // The regex to match the incoming URL
        'test/([^/]+)',
        // The resulting internal URL: `index.php` because we still use WordPress
        // `pagename` because we use this WordPress page

        'index.php?pagename=test',
        // This is a rather specific URL, so we add it to the top of the list
        // Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
        'top' );
}

它将加载带有参数的 /test/ 页面,而 javascript 将处理其余部分。

/test/ 之后的所有内容都被接受,没有 404 错误。偶数:/test/a/b/c/d/e

【讨论】:

    猜你喜欢
    • 2012-07-17
    • 2022-12-09
    • 2015-06-06
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多