Um dos novos recursos que adicionamos aqui no [wp24] é este recurso chamado “Explore“. Quando um usuário clica neste botão, eles são levados para um post aleatório do site. Anteriormente, queríamos apenas que os usuários fossem levados à página para exibir um post aleatório. Neste artigo, vamos mostrar-lhe como redirecionar usuários para um post aleatório no WordPress.
Abra o arquivo functions.php do seu tema, ou Plugin Específico do Site e cole o seguinte código:
add_action('init','random_add_rewrite'); function random_add_rewrite() { global $wp; $wp->add_query_var('random'); add_rewrite_rule('random/?$', 'index.php?random=1', 'top'); } add_action('template_redirect','random_template'); function random_template() { if (get_query_var('random') == 1) { $posts = get_posts('post_type=post&orderby=rand&numberposts=1'); foreach($posts as $post) { $link = get_permalink($post); } wp_redirect($link,307); exit; } }
É só isso e pronto. Agora crie um botão que link para “seudominio.com.br/random/” e o snippet acima vai cuidar do resto.
Explicação do snippet:
Este snippet primeiramente adiciona a variável “random“. Em seguida, ele usa o hook template_redirect do WordPress e diz se a variável “random” está lá, então, obtém um post do tipo “Post” de forma aleatória. Em seguida, ele faz um simples redirecionamento 307.
A razão pela qual o snippet usa o redirecionamento 307, é por ser um redirecionamento temporário. Os Browsers muitas vezes mantém o cache do redirecionamento 302, que é conhecido por redirecionamentos temporários.
Alguns bugs que encontramos
Se você estiver usando o plugin W3 Total Cache e tiver ativado o cache do banco de dados, então você precisa adicionar as seguintes regras na lista de exclusão.
/random/
/index.php?random=1
Se você estiver usando o plugin WordPress SEO do Yoast, então não use Redirecionar URLs feias para links permanentes limpos na área de Links Permanentes. Caso contrário, este código não vai funcionar.
Se você estiver tendo problemas com o redirecionamento indo para um erro 404 ao usar “seudominio.com.br/random/”, tente usar “seudominio.com.br/?random=1”.
Se quiser usar um plugin para fazer este redirecionamento, tente o Random Redirect. (Este plugin não é atualizado há algum tempo)
Agora você pode criar um pequeno botão Explore como o que temos em nosso site. Deixe sua opinião em um comentário abaixo e compartilhe este tutorial com seus amigos.
83 respostas
Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://accounts.binance.com/bg/register?ref=FIHEGIZ8
I am actually thankful to the owner of this website who has shared this wonderful post at here.
My homepage; vpn special coupon code 2024
My brother recommended I would possibly like this blog.
He used to be totally right. This submit actually made my
day. You can not imagine just how so much time I had spent for this information! Thanks!
Review my web page :: vpn 2024
п»їbest mexican online pharmacies: Mexican Pharmacy Online – mexican border pharmacies shipping to usa
top 10 pharmacies in india http://indiaph24.store/# online pharmacy india
top 10 pharmacies in india
https://canadaph24.pro/# certified canadian pharmacy
mexican pharmaceuticals online [url=http://mexicoph24.life/#]cheapest mexico drugs[/url] mexican rx online
pharmacies in mexico that ship to usa: Mexican Pharmacy Online – mexican drugstore online
http://indiaph24.store/# best india pharmacy
canadian drug pharmacy [url=https://canadaph24.pro/#]Certified Canadian Pharmacies[/url] canada drug pharmacy
benefits of tamoxifen: pct nolvadex – nolvadex pct
https://lisinopril.network/# zestril canada
buy cipro online without prescription [url=https://ciprofloxacin.tech/#]buy cipro[/url] buy cipro online without prescription
http://lisinopril.network/# lisinopril 40mg
buy cytotec over the counter [url=https://cytotec.club/#]Abortion pills online[/url] buy cytotec pills
cost generic propecia pill: buying propecia online – cost of cheap propecia price
https://nolvadex.life/# tamoxifen generic
https://cytotec.club/# buy cytotec
lisinopril prices: prinivil 2.5 mg – can i buy lisinopril over the counter in canada
http://cytotec.club/# buy misoprostol over the counter
http://nolvadex.life/# tamoxifen rash pictures
tamoxifen breast cancer [url=https://nolvadex.life/#]effexor and tamoxifen[/url] does tamoxifen cause joint pain
tamoxifen for men: how does tamoxifen work – tamoxifen hip pain
zestoretic price [url=http://lisinopril.network/#]buy prinivil online[/url] lisinopril rx coupon
https://cytotec.club/# cytotec pills buy online
https://cytotec.club/# buy cytotec over the counter
cipro generic: antibiotics cipro – buy cipro cheap
http://ciprofloxacin.tech/# buy cipro cheap
http://finasteride.store/# cost of cheap propecia without dr prescription
lisinopril brand name canada [url=https://lisinopril.network/#]lisinopril 80mg tablet[/url] lisinopril 3.5 mg
lisinopril 2.5 tablet [url=http://lisinopril.network/#]zestril 5 mg prices[/url] lisinopril 20mg
buy misoprostol over the counter: buy cytotec over the counter – Abortion pills online
https://finasteride.store/# order propecia without a prescription
order cheap propecia price [url=http://finasteride.store/#]propecia rx[/url] buying generic propecia
http://cytotec.club/# Abortion pills online
buy cytotec online fast delivery: buy cytotec in usa – buy cytotec over the counter
https://finasteride.store/# get cheap propecia
http://finasteride.store/# get propecia pills
canadian lisinopril 10 mg [url=http://lisinopril.network/#]purchase lisinopril 40 mg[/url] lisinopril 40 mg without prescription
aromatase inhibitor tamoxifen: nolvadex online – tamoxifen alternatives premenopausal
http://lisinopril.network/# average cost of lisinopril
buy cipro online without prescription [url=http://ciprofloxacin.tech/#]buy cipro[/url] п»їcipro generic
order cytotec online: buy cytotec – Cytotec 200mcg price
http://nolvadex.life/# benefits of tamoxifen
http://lisinopril.network/# 50 mg lisinopril
buy cytotec over the counter: purchase cytotec – order cytotec online
http://nolvadex.life/# nolvadex only pct
buying propecia no prescription [url=http://finasteride.store/#]order generic propecia no prescription[/url] buy propecia without dr prescription
ciprofloxacin generic: where can i buy cipro online – cipro ciprofloxacin
https://ciprofloxacin.tech/# cipro 500mg best prices
http://lisinopril.network/# buying lisinopril in mexico
Kamagra Oral Jelly: kamagra oral jelly – Kamagra 100mg
http://kamagra.win/# Kamagra 100mg
https://cenforce.pro/# Cenforce 100mg tablets for sale
Purchase Cenforce Online: cenforce for sale – cenforce for sale
cenforce.pro: cheapest cenforce – Buy Cenforce 100mg Online
https://levitrav.store/# Buy Vardenafil 20mg online
http://viagras.online/# buy viagra here
Cheap generic Viagra [url=https://viagras.online/#]Cheap Viagra 100mg[/url] Buy Viagra online cheap
Viagra tablet online: Buy generic 100mg Viagra online – Viagra online price
http://viagras.online/# Generic Viagra for sale
Generic Viagra for sale: Cheap Viagra 100mg – Viagra online price
http://cenforce.pro/# Cenforce 100mg tablets for sale
Viagra online price: buy Viagra over the counter – viagra canada
http://kamagra.win/# super kamagra
Levitra generic best price: buy Levitra over the counter – Vardenafil buy online
https://viagras.online/# buy Viagra online
http://cenforce.pro/# Buy Cenforce 100mg Online
Cheap Levitra online: levitrav.store – Buy Vardenafil 20mg
Viagra generic over the counter [url=https://viagras.online/#]Buy Viagra online cheap[/url] sildenafil online
india online pharmacy: reputable indian online pharmacy – india pharmacy mail order
http://pharmworld.store/# cheap pharmacy no prescription
http://pharmcanada.shop/# best canadian pharmacy to order from
canadian pharmacy without prescription: pharm world store – canadian pharmacy world coupon code
top 10 online pharmacy in india: indian pharmacies safe – indian pharmacy
indian pharmacy online: top 10 online pharmacy in india – india pharmacy
http://pharmcanada.shop/# canadian drugs online
purple pharmacy mexico price list [url=https://pharmmexico.online/#]mexican mail order pharmacies[/url] medication from mexico pharmacy
canadian pharmacy no scripts: canadian pharmacies online – canada pharmacy
http://pharmindia.online/# mail order pharmacy india
mexican drugstore online: buying from online mexican pharmacy – pharmacies in mexico that ship to usa
buying prescription drugs in mexico: reputable mexican pharmacies online – mexico pharmacies prescription drugs
http://pharmnoprescription.icu/# buy pain meds online without prescription
buy prescription drugs on line: best online prescription – no prescription needed pharmacy
http://pharmmexico.online/# best online pharmacies in mexico
legitimate canadian pharmacy online: drugs from canada – ed meds online canada
medicine in mexico pharmacies: mexican border pharmacies shipping to usa – mexico drug stores pharmacies