How to Add query strings to links in WordPress

Source: Internet
Author: User
This article describes how to add a query string to a link in WordPress by using the add_query_arg () function, you can refer to the query string after the question mark in the link. the format is key = value. Separate multiple query statements with the & symbol. The add_query_arg () function is a convenient function provided by WordPress. it can help you add one or more query statements to a link.

Someone may ask, can I add a query string directly after the link and manually append it? This is sometimes feasible, but if the link itself has a query string, it will cause format errors, and it is not conducive to PHP writing and management. the add_query_arg () function helps you think about everything, you only need to provide the link for adding the query string and one or more key values and key names.

Usage

add_query_arg( $key, $value, $old_query_or_uri );add_query_arg( $query, $old_query_or_uri );

Parameters

Add_query_arg () can be used in two ways. The first method requires three parameters.

$ Key

(Integer | string) (required) key value of the parameter to be added. like an array, if it is a string, it is the name, and the number is the nth parameter.

Default value: None

$ Value

(Mixed) (required) value to be added.

Default value: None

$ Old_query_or_uri

(String) (optional) link to be processed.

Default value: False. if it is set to False, the value is $ _ SERVER ['request _ URI '].

In the second method, two parameters are required to pass the query string to be added to the link using an array.

$ Query

(Array) (required) the query to be added. The key value corresponds to the key name.

Default value: None

$ Old_query_or_uri

(String) (optional) link to be processed.

Default value: False. if it is set to False, the value is $ _ SERVER ['request _ URI '].

Return value

(String) add the link to the query string.

Example

First usage:

echo add_query_arg( 'id', 123, 'http://www.bitsCN.com' );

Second usage:

echo add_query_arg( array( 'id' => 123 ), 'http://www.bitsCN.com' );

Are both results of http://www.bitsCN.com? Id = 123

Other special cases:

echo add_query_arg( array( 'id' => 123 ), 'http://www.bitsCN.com?query=post' );//http://www.bitsCN.com?query=post&id=123echo add_query_arg( array( 'id' => 123, 'cat' => 'themes' ), 'http://www.bitsCN.com?query=post' );//http://www.bitsCN.com?query=post&id=123&cat=themesecho add_query_arg( array( 'id' => 123, 'cat' => 'themes', 'query' => 'page' ), 'http://www.bitsCN.com?query=post' );//http://www.bitsCN.com?query=page&id=123&cat=themes

Example

This function is located in: wp-functions des/functions. php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.