Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Kupası
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPress Arama Sorgusu Yalnızca Bir Eşleşme Döndürdüğünde Otomatik Yönlendirme

Bazen bir WordPress blogunda arama yaparken sonuçta yalnızca bir öğe görürsünüz. Sitenizin ne kadar düzenli olduğuna bağlı olarak, bu zaten kullanıcının aradığı öğe olmalıdır. Kullanıcılarımızdan biri, arama sorgusu yalnızca bir eşleşme ile sonuçlanırsa gönderiye yönlendirmenin bir yolu olup olmadığını sordu. Bu makalede, arama sorgusu yalnızca bir eşleşme döndürdüğünde kullanıcıları gönderiye nasıl yönlendireceğinizi göstereceğiz.

Tek yapmanız gereken temanızın functions.php dosyasını açmak ve aşağıdaki parçacığı yapıştırmaktır.

add_action('template_redirect', 'one_match_redirect');
function one_match_redirect() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
        }
    }
}

Şimdi, bazı kullanıcıların bu işlevi beklemeyeceklerine dikkat etmelisiniz. Bu yüzden onları korkutabilir.

Açıklama: İçeriğimiz okuyucu desteklidir. Bu, bazı bağlantılarımıza tıklarsanız komisyon kazanabileceğimiz anlamına gelir. WPBeginner'ın nasıl finanse edildiğini, neden önemli olduğunu ve nasıl destek olabileceğinizi görün. İşte editoryal sürecimiz.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Ultimate WordPress Araç Kiti

Araç setimize ÜCRETSİZ erişim sağlayın - her profesyonelin sahip olması gereken WordPress ile ilgili ürün ve kaynaklardan oluşan bir koleksiyon!

Reader Interactions

7 yorumBir Cevap Bırakın

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Casey Lessard says

    Worked for me. I run a local search engine using WordPress and I have been trying to find this solution for some time. Does exactly as described. Thank you so much!

  3. Eric says

    I want to make it so when some searches a specific phrase it redirects to an easter egg, like if someone searches “I see dead people” specifically it’ll redirect them to a URL I can define.

  4. Nebulas Website Design says

    Thanks for this piece of code it made my client very happy and I didn’t thnk it could be done. However it’s not working for us we are using the Relevanssi search plugin should it work with this?

  5. carlos says

    What if I want to redirect any search query to the most relevant post no matter what the number of posts the search returns? I’d really like to be able to do that.

Bir Cevap Bırakın

Yorum bırakmayı seçtiğiniz için teşekkür ederiz. Lütfen tüm yorumların yorum poli̇ti̇kasi uyarınca denetlendiğini ve e-posta adresinizin yayımlanmayacağını unutmayın. Ad alanında anahtar kelime KULLANMAYIN. Kişisel ve anlamlı bir sohbet edelim.