Wordpress最新的帖子不能抓取当前的URL


Wordpress latest posts can't grab current URL

我截取了下面的代码来获取当前页面的URL…

                <?php
                function curPageURL() {
                 $pageURL = 'http';
                 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
                 $pageURL .= "://";
                 if ($_SERVER["SERVER_PORT"] != "80") {
                  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
                 } else {
                  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
                 }
                 return $pageURL;
                }
            ?>

这样我就可以把当前页面的URL放入我的模板的Facebook like按钮…

        <div class="fb-like" data-href="<?php echo curPageURL(); ?>" data-layout="button" data-action="like" data-show-faces="true" data-share="true"></div>

这在我的Wordpress网站的所有页面上都很好,但我在我的最新帖子页面上得到一个错误。我不能在没有得到这个错误的情况下发布多个帖子…

致命错误:无法在/home4/whimint/public_html/whetink.co/wp-content/themes/kingdom/content.php中重新声明curpageurl()(先前在/home4/whimint/public_html/whetink.co/wp-content/themes/kingdom/content.php中声明:81)

任何想法吗?让我知道,如果你需要更多的细节,我是新的堆栈溢出。提前感谢!

将函数移动到functions.php文件中,这样它只声明一次。你也可以用this来包装它,以防止它被声明两次。

if(!function_exists("curPageURL")){
function curPageURL() {
   $pageURL = 'http';
   if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
        $pageURL .= "://";
   if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
   } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
   }
    return $pageURL;
}
}

致命错误:无法在/home4/whimint/public_html/whetink.co/wp-content/themes/kingdom/content.php中重新声明curpageurl()(先前在/home4/whimint/public_html/whetink.co/wp-content/themes/kingdom/content.php中声明:81)

这个错误说,你的函数在你的文件(content.php)中出现了两次。第81行一次,第90行一次。

您的代码以驼峰形式显示函数,错误以小写形式显示。PHP函数不区分大小写。您是否尝试使用相同(不区分大小写)名称的多个函数?