如果页面无法回显自定义标题
i have a posttype to which i have a page templates assigned using plugin custom posttype page template, so that my posts having img attachments in the posttype are displayed using a particular template. In my header.php i have code
if ( is_page_template ( 'page-book.php' ) )
{ $currenturl=$_SERVER['REQUEST_URI'];
$booktitle = str_replace("http://mybooks.com/books/", "", $currenturl);
$booktitle = str_replace("-", " ", $booktitle);
$booktitle = strtoupper($booktitle);
echo $booktitle;
}
but it echoes the url of the post in the title instead of the custom echo
how do i get it to echo the custom title
我有一个帖子类型,我有一个使用插件自定义帖子类型页面模板分配的页面模板,以便我的帖子有 使用特定模板显示posttype中的img附件。 在我的header.php中,我有代码 p>
if(is_page_template('page-book.php'))
{$ currenturl = $ _ SERVER ['REQUEST_URI']; \ n $ booktitle = str_replace(“http://mybooks.com/books/”,“”,$ currenturl);
$ booktitle = str_replace(“ - ”,“”,$ booktitle);
$ booktitle = strtoupper ($ booktitle);
echo $ booktitle;
}
code> pre>
但它回显标题中帖子的url而不是自定义echo p >
如何让它回显自定义标题 p>
div>
This line $currenturl = $_SERVER['REQUEST_URI'];
of your code only return string that is part of the URL
.
For example, the given url http://mybooks.com/books/title
it return /books/title
and when you try to replace it with an empty string nothing is replace because str_replace
function doesn't find a match.
For you to get the trailing part of the url, you can use basename
For example
echo basename($currenturl);
It will return the last part of the url and that is
title
.
Your code seems to work for me. Try wit these solutions:
Solution 1:
$currenturl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$booktitle = str_replace("http://mybooks.com/books/", "", $currenturl);
$booktitle = str_replace("-", " ", $booktitle);
$booktitle = strtoupper($booktitle);
echo $booktitle;
or try debugging it with something like this:
Solution 2:
$currenturl = "http://mybooks.com/books/this-is-the-book-title";
$booktitle = str_replace("http://mybooks.com/books/", "", $currenturl);
$booktitle = str_replace("-", " ", $booktitle);
$booktitle = strtoupper($booktitle);
echo $booktitle;