我没试过你的代码,但它似乎已经有一个双echo
线..
无论如何,我想给你一个非常简单的方法,我喜欢用:
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;
}
这个功能的作用是每当你打电话给curPageURL()
时,你就会得到你当前的网页网址,就像它在网址栏里看到的一样。比你可以随心所欲地工作。
我希望这能解决你的问题:)
编辑:
要使用该用于获取URL和比对了变化,从.html
到.pdf
所有你需要做的结局:
$url = curPageURL(); // gets back www.sample.com/page.html
$newurl = str_replace(".html", ".pdf", $url); // changes url to www.sample.com/page.pdf
该代码使用顶级函数
编辑2:
如果返回你:www.mysite.com/01/05/2013/9013/your-mom-wears-combat-boots/
比你需要做的:
$url = curPageURL(); // gets back www.sample.com/page/
$newurl = substr_replace($url, "", -1) // changes url to www.sample.com/page
$newurl .= ".pdf" // adds the .pdf to the url so you get www.sample.com/page.pdf
编辑3:
首先的 - 很高兴你使用WordPress :)
现在,wordpress codex使得它更容易,实际上更容易做到: 为此我们需要进行一些更改,同时更改curPageURL函数。
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"];
}
else {
$pageURL .= $_SERVER["SERVER_NAME"];
}
return $pageURL;
}
,我会解释:
$_SERVER["SERVER_NAME"]
给我们的域名,如domain.com
。
$_SERVER["REQUEST_URI"]
向我们返回域,路径后的所有内容。在我们的例子中为 ,例如:01/03/2013/9013/page
。
所以现在curPageURL
函数只返回域名。
,现在让我们得到什么是未来的工作:
$url = curPageURL(); // gets back http://www.sample.com
$url .= "/pdf"; // we add the /pdf to the url so it is now: http://www.sample.com/pdf
$postid = get_the_ID(); // gives us the post ID. for example 9013
$url .= "/" . $postid; // now we take the post ID and add it to the url: http://www.sample.com/pdf/9013
$the_post = get_post($postid); // get the post by the ID
$post_slug = $the_post['post_name']; // get the slug of the post
$url .= "/" . $post_slug; // adds the slug to the url: http://www.sample.com/pdf/9013/your_mom_wears_combat_boots
$url .= ".pdf"; // finally add the pdf to the url so now you get: http://www.sample.com/pdf/9013/your_mom_wears_combat_boots.pdf
这里是没有注释:
$url = curPageURL();
$url .= "/pdf";
$postid = get_the_ID();
$url .= "/" . $postid;
$the_post = get_post($postid);
$post_slug = $the_post['post_name'];
$url .= "/" . $post_slug;
$url .= ".pdf";
,如果你想将线最小化:
$url = curPageURL() . "/pdf/" . get_the_ID(); // url is now: www.domain.com/pdf/postid
$the_post = get_post(get_the_ID());
$url .= "/" . $the_post['post_name'] . ".pdf"; //url is now: www.domain.com/pdf/postid/post_slug.pdf
现在你可以echo
该网址通过echo $url;
更新,最后编辑(4):
我犯了一个错误,有关获取蛞蝓。
我用过:$the_post = get_post(get_the_ID());
而且拿了slu,,名字:$the_post['post_name']
这是不对的。
采取蛞蝓是最好的出路是通过运行代码:
$post_slug = basename(get_permalink());
所以这chagnes一点点它supposd寻找方式:
$url = curPageURL() . "/pdf/" . get_the_ID(); // url is now: www.domain.com/pdf/postid
$post_slug = basename(get_permalink()); //gets the post slug
$url .= "/" . $post_slug . ".pdf"; //url is now: www.domain.com/pdf/postid/post_slug.pdf
,我们可以把它越来越最小化:
$url = curPageURL() ."/pdf/". get_the_ID() ."/". basename(get_permalink()) .".pdf";
,现在你可以echo $url;
请不要忘记:此功能需要位于page.php
或single.php
中,才能知道帖子的内容和帖子的ID。
行:$url = curPageURL() ."/pdf/". get_the_ID() ."/". basename(get_permalink()) .".pdf";
需要在循环:<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
或您使用的任何其他循环。
这应该做的工作:)
你的错误是两个'echo's彼此相邻。 – apartridge