【问题标题】:Add prefix to link url in wordpress在wordpress中为链接url添加前缀
【发布时间】:2015-10-03 08:13:54
【问题描述】:

我想在 wordpress 网站上添加图片链接,将用户重定向到相同 url 但前缀不同的另一个页面。例如 页面 url 是 mydomain.com/post1234 当用户单击此页面上的图像时,它将用户重定向到 url mydomain.com/md/post1234

以下代码在网站上的每个帖子页面上打印当前页面 url,但我想在 url 中添加“md”前缀

<a href="<?php
$Path=$_SERVER['REQUEST_URI'];
$URI='http://www.example.com'.$Path;
?>"><img src="<?php bloginfo('template_url'); ?>/images/sgxx.png">Click Here</a>

请建议正确的代码来执行此操作。

【问题讨论】:

    标签: php wordpress permalinks


    【解决方案1】:

    试试这个:

    <?php
        $domain = get_site_url(); //You can use this or either "http://".$_SERVER[HTTP_HOST];
        $path = $_SERVER[REQUEST_URI];
        $prefix = "/md";
    ?>
    <a href="<?= $domain.$prefix.$path ?>">Link</a>
    

    有关$_SERVER的更多信息,请查看以下链接: http://php.net/manual/en/reserved.variables.server.php

    【讨论】:

    • 谢谢@Clyff 它解决了我的问题。我该怎么做,反之亦然。如果我想从前缀页面上的链接中删除前缀。比如登陆页面 mysite.com/md/post1234 到 mysite.com/post1234 的超链接
    • 查看此答案是否有类似问题:stackoverflow.com/questions/12061528/… 如果您需要更多帮助,请告诉我。
    • 我用反之亦然&lt;a href=" &lt;?php $url = get_site_url(); $path = $_SERVER[REQUEST_URI]; $newstring = str_ireplace("/md", "", $url.$path ); echo $newstring ?&gt;"&gt;&lt;img src="&lt;?php bloginfo('template_url'); ?&gt;/images/sgxx.png"&gt;Click Here&lt;/a&gt; 效果很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 2017-06-09
    • 2014-01-26
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多