[wp-hackers] Appending Query Strings to Pretty Permalinks

Chip Bennett chip at chipbennett.net
Mon Mar 7 19:39:33 UTC 2011


Good afternoon, hackers,

I am attempting to create a "Single-Page View" link for paginated posts. I
have everything working except for one: appending the correct query string
delimiter.

Here's the relevant code:

global $page;
if ( is_singular() && $page ) {
$query_var_symbol = ( isset( $_SERVER['QUERY_STRING'] ) ? '&' : '?' );
$single_page_permalink = get_permalink() . $query_var_symbol . 'all=1'; ?>
<strong>|</strong> <a href="<?php echo $single_page_permalink;
?>">Single-Page View</a>
<?php } ?>


I've got this code to determine which delimiter to use:

$query_var_symbol = ( isset( $_SERVER['QUERY_STRING'] ) ? '&' : '?' );


But it always returns true, which results in the appended query string
always being delimited with "&".

My guess is that, because the actual URL is e.g. "www.domain.tld/?p=123",
the $_SERVER variable is returning true in all instances.

This results in, e.g. "www.domain.tld/2011/post-name/&querystring" instead
of "www.domain.tld/2011/post-name/?querystring"

The end result appears to work, but I'd still like to know:

1) How do I properly determine the appropriate query string delimiter?
2) Is there a better way to do what I'm doing? (I generally try to avoid
using $_SERVER variables.)

And if this question is more properly asked in the WPORG support forums,
just let me know and I'll repost there.

Thanks,

Chip


More information about the wp-hackers mailing list