开发者

Wordpress PHP if statement to detect URL and server specific header.php page

In WordPress I would like to to show a custom header.php if the URL contains a specific string (in开发者_如何学Go this case /support/ also to include /support/forums/). I have looked around and have tried many different combinations, but cannot seem to get it to work. Below is the code I have, any help would be greatly appreciated!!!

if (substr($_SERVER['REQUEST_URI'], 0, 5) !== 'support') {
get_header('XXXXXX');
} else {
get_header();
}

FYI... I need it to work this way (I think) because I am using custom post types and therefore cannot use standard WordPress calls because it is just a slug and not a Post/Category/.etc.

Thanks!!


Another very similar way would be:

<?php $uri_segments = explode( '/', $_SERVER['REQUEST_URI'] );
if ( in_array('support', $uri_segments) ) { get_header('xxxx'); }else{ get_header(); }
?>


Did you forget your code? Nothing is showing up.

Anyways $_SERVER['QUERY_STRING'] is the value you are after, you can then use PHP's string manipulating functions like explode() or better yet strpos() to find and react to whatever you are after, find more about the function explode PHP Manual page and strpos PHP Manual page and about the _SERVER reserved variable


Supposing your URI has the 'support' string always at that position (first), your code looks fine, I can't see any error there. But remember that the argument of the get_header() function is the suffix that you must append to the file name. So, if you have:

get_header('test');

Your header file should be named: header-test.php

Also, to be sure you always find the string even if it changes position in the URI, you could do it like this:

$params= explode('/',$_SERVER['REQUEST_URI'] );

if(in_array('support', $params)) {
    get_header('support');
} else {
    get_header();
}


  1. The length of the compared string, support is not 5 characters long, so the if statement is always false.

  2. The URI normally starts with a / at position 0, so include it in the string to compare or start comparing at position 1.

I would use:

if (substr($_SERVER['REQUEST_URI'], 0, 9) !== '/support/') {
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜