我的WordPress网站有一个自定义帖子类型"car",所以我有类似www.example.com/car/honda-civic-2013/
现在我还有一个操作"购买",使用add_rewrite_endpoint()
显示特定信息:www.example.com/car/honda-civic-2013/buy
在我的header.php
模板中,检查当前页面是否不是"购买"的适当方法是什么?
这是我所拥有的:
if (is_singular('car')) {
echo "Click here to buy this car";
}
它可以工作,但它也会在/buy 页面上显示"单击此处购买这辆车",我想避免!
我想要一个永久的解决方案,这意味着我不想特别检查"买入",而是检查任何/sub-action,因此上述内容仅在 CustomPostType 主页上回显。
感谢您的任何帮助
这比您可能希望的代码要重一些,但这应该有效:
// Get the current URL
$url = @( $_SERVER['HTTPS'] != 'on' ) ? 'http://' . $_SERVER['SERVER_NAME'] : 'https://' . $_SERVER['SERVER_NAME'];
$url .= ( $_SERVER['SERVER_PORT'] !== 80 ) ? ":" . $_SERVER['SERVER_PORT'] : '';
$url .= $_SERVER['REQUEST_URI'];
// If current URL is for cars and not the "buy" page
if ( is_singular('car') && !strpos($url,'/buy') ) {
echo "Click here to buy this car";
}
来源:
- https://gist.github.com/cferdinandi/6608505
- 检查URL是否具有PHP的某些字符串
global $wp_query;
if (is_singular('car') && !isset($wp_query->query['buy'])) {
echo "Click here to buy this car";
}