내가 코드를 시도하지 않은하지만 이미 이중 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;
}
을 이 기능은 URL 바에있는 것처럼 현재 페이지 URL을 가져 오는 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 :
당신을 반환하는 경우 :
$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 : 당신이해야 할 것보다 www.mysite.com/01/05/2013/9013/your-mom-wears-combat-boots/
모든
제일 - 다행 당신이 사용하는 워드 프레스 :
이제는 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
URL을로 만들 수 있습니다. 98,
업데이트, 마지막 편집 (4) : 나는 슬러그를 얻기에 관하여, 실수를 한
.
나는 사용했다 : $the_post = get_post(get_the_ID());
그리고 슬러그를 가지고 간, 이름 : $the_post['post_name']
이것은 바르지 않았다.
$post_slug = basename(get_permalink());
그래서 이것은 조금에게 그것을보고 supposd 방법 chagnes :
$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;
할 수 있습니다 더욱 최소화
잊지 말아야 할 점 :이 함수는 게시물의 슬러그와 게시물의 ID를 알아 내기 위해 page.php
또는 single.php
이어야합니다.
줄 : $url = curPageURL() ."/pdf/". get_the_ID() ."/". basename(get_permalink()) .".pdf";
은 루프에 있어야합니다 : <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
또는 다른 루프를 사용해야합니다.
이 작업을 수행해야합니다 :)
오류는 두 개의 서로 다른 옆에 있습니다. – apartridge