Using WordPress random redirect function skillfully

Source: Internet
Author: User

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

Today, radish fish looking for information on Wikipedia, see a wiki on the function of a random article is very interesting, summed up this kind of random article in WordPress, the implementation of the way.

  

Through the random redirection function, the reader is given an opportunity to jump randomly to any article. The real way to achieve this is simple. All you need to do is just create a hyperlink, named "Random article," and redirect the random article when clicked.

In order to achieve random redirection in WordPress, you can have the following three steps:

· A page to handle redirection

· After the query has been selected from the database,

· Redirect the user to a random article through a link.

Of course, you can use a plugin. Radish fish do not recommend this approach if you do not need other features.

1, using the get_posts () function to achieve redirection

Create a page-random.php template page, page-random.php does not include the header area, sidebar and footer template loading, because page-random.php will not produce any visible output for the user, it will only jump (that is, redirect) To the randomly selected articles we set. The implementation process is: Select an article randomly from the database, use a foreach loop to process the output, redirect the user to a random article.

page-random.php Source Code

Random redirect page template

Set parameters for Get_posts ()

$args = Array (

' Numberposts ' => 1,

' => ' Rand '

);

Randomly select an article from a database

$my _random_post = get_posts ($args);

Process database requests through a foreach loop

foreach ($my _random_post as $post) {

redirect user to random article

Wp_redirect (Get_permalink ($post->id));

Exit;

}

Now what we need to do is go to WordPress's admin section to create a new random-named blank page for the page-random.php template and publish it. Then, for example, when you visit http://www.luoboju.com/random/, you are automatically redirected to a random article. Before you can create a "random article" hyperlink like Wikipedia, clicking the hyperlink will bring the reader to a random article you set.

  

2, using wp-query () to achieve redirection

Page-random.php source code (implemented by Wp_query)

Random redirect page template

Set parameters for Wp_query ()

$args = Array (

' Posts_per_page ' => 1,

' => ' Rand '

);

Randomly select an article from a database

$my _random_post = new Wp_query ($args);

Processing database requests through Wp_query

while ($my _random_post->have_posts ()) {

$my _random_post->the_post ();

redirect user to random article

Wp_redirect (Get_permalink ());

Exit;

}

The biggest advantage of using wp_query is that it has more parameters than the get_posts () function, which provides greater flexibility when you create specific queries.

Below the radish fish to cite several examples:

We can let WordPress redirect to a certain category of articles, such as we redirect to the home network of radish below the "HTML template" of this category of any article:

Set parameters for Wp_query ()

$args = Array (

' Category_name ' => ' htmlmuban ',//Remember, use category alias.

' Posts_per_page ' => 1,

' => ' Rand '

);

Randomly select an article from a database

$my _random_post = new Wp_query ($args);

Processing database requests through Wp_query

while ($my _random_post->have_posts ()) {

$my _random_post->the_post ();

redirect user to random article

Wp_redirect (Get_permalink ());

Exit;

}

What if we were to exclude the "HTML Template" category and redirect to any of the remaining categories below? How to achieve:

Set parameters for Wp_query ()

$args = Array (

' Category__not_in ' => Array (1),//Here is the category ID to exclude, such as HTML template This category ID is 1, where we exclude him

' Posts_per_page ' => 1,

' => ' Rand '

);

Randomly select an article from a database

$my _random_post = new Wp_query ($args);

Processing database requests through Wp_query

while ($my _random_post->have_posts ()) {

$my _random_post->the_post ();

redirect user to random article

Wp_redirect (Get_permalink ());

Exit;

}

From these examples, you can see that there is no complexity, and nothing too advanced, we can use a few lines of code to achieve random redirect WordPress to any article function. You can also extend more uses in the above way. Hey, let's talk about this today, this article by Radish home Net http://www.luoboju.com original release, reprint please specify, thank you.

Related Article

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.