0

My WordPress site has a Custom Post Type "car", so I have something like www.example.com/car/honda-civic-2013/

Now I have also an action "buy" using add_rewrite_endpoint() displaying specific information: www.example.com/car/honda-civic-2013/buy

In my header.php template, what is the appropriate way to check that the current page is not "buy"?

Here's what I have:

if (is_singular('car')) {
    echo "Click here to buy this car";
}

It works but it also displays "Click here to buy this car" on the /buy page which I want to avoid!

I want a permanent solution, meaning that I don't want to check against "buy" in particular, but check against ANY /sub-action so the above is echoed only on the main CustomPostType page.

Thank you for any help

4

2 回答 2

0

这比您可能想要的代码要重一些,但这应该可以工作:

// 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";
}

资料来源:

于 2013-10-23T02:46:34.050 回答
0
global $wp_query;
if (is_singular('car') && !isset($wp_query->query['buy'])) {
    echo "Click here to buy this car";
}
于 2013-10-23T04:29:49.540 回答