[wp-hackers] Appending Query Strings to Pretty Permalinks

Chip Bennett chip at chipbennett.net
Mon Mar 7 20:04:28 UTC 2011


I KNEW there had to be a WP-supported way.

So, something like this:

echo add_query_arg( 'all', '1', get_permalink() );

Thanks!

Chip

On Mon, Mar 7, 2011 at 1:56 PM, John Blackbourn <johnbillion+wp at gmail.com>wrote:

> add_query_arg()[1] is your friend. Example:
>
> $single_page_permalink = add-query_arg( array( 'all', '1' ),
> get_permalink() );
>
> [1] http://codex.wordpress.org/Function_Reference/add_query_arg
>
> On 7 March 2011 19:39, Chip Bennett <chip at chipbennett.net> wrote:
> > 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
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> >
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list