【问题标题】:How to redirect based on URL如何根据 URL 重定向
【发布时间】:2014-10-17 10:37:39
【问题描述】:

我正在使用 Wp Store Locator 插件。我已根据需要对其进行了修改。但我陷入了重定向。

这个插件基本上可以使用短代码。所以在列出我的 URL 时是这样的:localhost/wordpress/?page_id=391

现在有一个链接将我重定向到http://localhost/wordpress/?wpsl_id=3

这是代码:

    add_action("template_redirect", 'my_theme_redirect');

    function my_theme_redirect() {

            $dpath = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ;
        $templatefilename = 'single-store.php';
        if (strpos($dpath,'wpsl_id') !== false){    
                $return_template = get_template_directory() .'/' .$templatefilename  ;
                //$nurl = get_permalink($return_template);
                //echo $return_template; exit;
                wp_redirect($return_template);

                exit;               
            }
    }

此代码不会将我重定向到任何地方。它停留在同一页面上 localhost/wordpress/?page_id=391 可能是什么问题?

任何人都可以建议我在 URL 中检测到 wpsl_id 时如何定向到 URL?我想将它们重定向到插件的模板文件夹,我已经为此添加了一个文件。

已编辑

我在短代码上方的插件文件中添加了上述代码。希望我与这个问题无关:)

【问题讨论】:

  • 您的 wp_redirect() 函数正在重定向到页面模板,而不是有效的站点 URL...
  • 你必须使用template_include过滤钩子而不是template_redirectcodex.wordpress.org/Plugin_API/Filter_Reference/…
  • @rnevius 对不起,我没有得到你。我错过了什么?或者我在哪里犯错了?
  • @jogesh_pi 您的建议不起作用.. 实际上它没有显示任何内容。只有空白页。

标签: wordpress redirect plugins


【解决方案1】:

模板名称看起来不错。您只需要在调用模板之前注册模板。

添加此过滤器,以便在您的操作之前执行:

add_filter('template_include', 'single-store');

【讨论】:

  • 您对 Wordpress 很陌生。请给出有用的答案或在回答之前先搜索一下,否则您将投反对票。
  • Dude 第二个参数必须是函数,而不是页面名称。感谢您的时间。我得到了解决方案,
  • 使用 template_include 钩子添加模板后,您可以正确调用模板,这是人们试图告诉您的。
猜你喜欢
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 2016-06-22
  • 1970-01-01
相关资源
最近更新 更多