Host Unlimited Web Sites for Only $10 per Month! 主機無限網站的只有10元每月!
Powered by MaxBlogPress 本站由MaxBlogPress

WordPress的哈克# 3 :自定義搜索結果頁面和模板| JTPRATT的博客誤區
JTPratt的博客錯誤





Home首頁 » Wordpress Hack #3: Custom Search Results Page and Template » WordPress的哈克# 3 :自定義搜索結果頁面和模板



Posted in: 張貼於:

博客安裝類形象WordPress的圖像類
2,134 views 2134意見


Please note: This page was originally written in English.請注意:此網頁最初是用英文寫的。

The original post can be viewed原來的文章可被視為 here在這裡 .

Please note: This page was originally written in English.

The text has been translated using an online service such as Google or Babelfish.

The original post can be viewed here.


When visitors search your Wordpress blog are they finding what they need? 當您的訪問者搜索WordPress的博客,他們發現他們需要什麼? Did you know you could monetize your search results? 你知道你可以賺錢的搜索結果嗎? This “wordpress hack” will teach you how to build your own custom search results page and template . 這種“ WordPress的黑客”將教你如何建立自己的自定義搜索結果頁面和模板。

This is Day #3 of our Wordpress “Hack-a-thon” of這是# 3日我們的WordPress的“哈克一村”的 30 Wordpress Hacks in 30 Days 30 WordPress的技巧30天內 . If you haven’t already - now is the time to subscribe by email or RSS at the top of the page!如果您尚未-現在是時候訂閱通過電子郵件或R SS上方的頁面!

Every Wordpress blog has a search feature, and when people enter keyword into that search box they expect to find relevant results.每個WordPress的博客有一個搜索功能,當人進入到該關鍵字搜索框,他們期望找到相關的結果。 I’m going to teach you how search works behind the scenes in Wordpress, and how you can customize it for your blog.我要教你如何搜索在幕後工作的WordPress的,以及如何您可以自定義為您的博客。 First let me say that Wordpress has it’s own search mechanism - and it doesn’t use google or other search engines for results - it searches posts and post titles within your database.首先我要說的是, WordPress的有它自己的搜索機制-它不使用谷歌或其他搜索引擎的結果-它搜索職位和職稱在您的數據庫。 If you were interested in incorporate google custom search results in your Wordpress theme and template this is not the place.如果你有興趣把谷歌定制搜索結果中的WordPress的主題和模板,這是不是地方。 Instead visit相反訪問 Google Custom Search in Wordpress谷歌定制搜索在WordPress的 . There was a time when I used google results instead of Wordpress search in my blog and I just don’t recommend it.曾經有一段時間,我用谷歌的結果而不是WordPress的搜索我的博客,我只是不推薦。 First, they advertise that it will make you money, but on a site getting 10,000 unique visits per day I only made between $1.50 and $2.00 per month.首先,他們做廣告,它會使你錢,而是在網站上獲得獨特的訪問10000每天我只之間1.50美元和2.00美元每月。 Second, google will only show pages you have indexed in your results.其次,谷歌將只顯示您的網頁索引中的結果。 What if all your pages aren’t in the google index?如果您所有的網頁不屬於谷歌的索引嗎?

The first step in customizing search in Wordpress is understanding the elements involved:第一步定制搜索WordPress的是理解所涉及的要素:

  • searchform.php : The searchform is not a page, it’sa template file. searchform.php :該searchform不是一個網頁,這是一個模板文件。 Whenever you see a search box on a blog page, this search form template file is called and it’s code is used to place the search box on the page.每當你看到一個搜索框的博客頁面,此搜索表單模板文件被稱為它的代碼是用來放置搜索框的網頁。 If you ever wanted to change or style your search box, this file is the one you would edit to do it.如果你想改變或樣式您的搜索框,這個檔案是一個你會編輯這樣做。 We’re not going to be changing this page at all today.我們不打算改變這一頁,今天所有。
  • search.php : The search.php page is also a template file, the one that’s used any time search results are displayed. search.php : search.php頁的也是一個模板文件,一個是任何時候使用搜索結果的顯示。 Since most Wordpress themes have a search box somewhere on every page you can usually search anywhere.由於大多數WordPress的主題有一個搜索框某處在每個網頁上通常可以搜索任何地方。 The results that come back use this template file結果是回來使用這個模板文件
  • searchpage.php : More than likely your blog doesn’t contain it’s own “search page” specifically because of the previous point (you can search anywhere). searchpage.php :以上您的博客可能不包含它自己的“搜尋網頁”明確,因為以前的點(您可以搜索任何地方) 。 There is however, no reason not to create one.然而,沒有理由不創建一個。 It’s yet again another page to get indexed in the search engines, yet another page to place original content, yet another page to monetize, and yet another page to point people to important content throughout your site.這是又一次另一個頁面得到索引的搜索引擎,但另一網頁將原始內容,但另一頁賺錢,但其他網頁指向人的重要內容您的網站。 We are going to create a custom search page or “searchpage.php” today in this tutorial.我們要創建一個自定義的搜索頁或“ searchpage.php ”今天在這個教程。

As I mentioned, we won’t be customizing the search form today, but we will be creating a custom search page and customizing the search results template.正如我前面提到,我們將不會定制搜索表單今天,但我們將創建自定義的搜索頁和定制的搜索結果模板。 First we’ll make the search page.首先,我們要讓搜索頁。 When completed, this will be a page included in our “Pages” block in the sidebar.當完成,這將是一個網頁包括在我們的“網頁”區塊中的側欄。 Let’s start at the the usual place, the讓我們先在通常的地方, Wordpress Codex page for ‘Creating a Search Page’ WordPress的法典頁'創建一個搜索頁' . It’sa pretty straightforward tutorial for creating a Wordpress search page.這是一個非常簡單的教程創造一個WordPress的搜索頁。 You going to download your “page.php” file from your Wordpress theme directory.你要下載您的“ page.php ”文件從您的WordPress的主題目錄。 If your theme doesn’t have one - follow the directions in the previous link for creating one.如果您的主題不會有一個-按照指示在過去的聯繫創造一個。 Save that file as “searchpage.php”.保存該檔案的“ searchpage.php ” 。

At the top of the page before anything we need to declare the template name - copy and paste this code:在頁面頂部的東西之前,我們必須宣布模板名稱-複製並粘貼此代碼:

 <?php /* Template Name: Custom Search Page */ ?> 

Next you need to any information in “the loop” from this page which you might not need like the meta information (date posted, comments, tags or categories, etc).下一步你需要的任何信息在“循環”從這個網頁你可能不必像元信息(發布之日起,評論,標籤或類別等) 。 I can’t tell you what will be in your theme, because every theme is different.我不能告訴你這將是在您的主題,因為每一個主題是不同的。 I’ll show you mine to use as an example in just a second.我會告訴你使用地雷作為一個例子在短短一秒鐘。 You need to add in a heading and search form by pasting these lines (after ‘the loop’):您需要添加的標題和搜索形式的粘貼這些行(後'環' ) :

 <h3>Search This Blog</h3> <?php include (TEMPLATEPATH . '/searchform.php'); ?> 

Save “searchform.php” and upload it to your Wordpress theme directory.保存“ searchform.php ” ,並把它上傳到你的WordPress的主題目錄。 Your file should look something like this at this point:您的文件看起來應該像這樣在這一點上:

 <?php /* Template Name: Custom Search Page */ ?> <?php get_header(); ?> <div id="wrapper"> <div id="content"> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <div class="post" id="post-<?php the_ID(); ?>"> <h2 id="post-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2> <br /> <div class="entrytext"> <?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?> <?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?> </div> </div> <?php endwhile; endif; ?> <h3>Search This Blog</h3> <br /> <?php include (TEMPLATEPATH . '/searchform.php'); ?> <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?> </div> <?php get_sidebar(); ?> </div> <?php get_footer(); ?> 

自定義搜索模板的例子 Congratulations you just created a (search) page template!恭喜您剛剛創建了(搜索)網頁模板! Now we’re going to create an actual “page” that uses that template.現在我們要創建一個實際的“頁面”使用該模板。 Go to your Wordpress dashboard to “Write -> Page”.轉到您的WordPress的儀表板為“收件-> ”頁“ 。 On the right, as in the image example here change “Page Template” to ‘Custom Search Page’.論權利,在圖像的例子在這裡改變“頁面模板” ,以'自定義搜索頁面。 The template we created contains the search form, heading, and later is where we’ll place any advertisements.我們的模板創建包含搜索的形式,標題,後來是我們將放置任何廣告。 The Wordpress “page” we’re creating now (that is based on that template) is where we’re going to write the actual content that goes on the page.在WordPress的“頁”我們現在創造(即是基於該模板)是我們要寫入的實際內容,不用在網頁上。 This would be a great time to exploit your best pages while creating some original content.這將是一個巨大的時間利用你最好的網頁,同時創造一些原始內容。

The content you write should be something like:在你寫的內容應該是這樣的:

“Welcome to ‘X’ Blog where I write about (link to) category a, (link to) category b, (link to) category c, etc. You might also be interested in x and y article series, my z products, or (link to) advertising on this site. “歡迎的' X '的博客,我寫的(鏈接)一個類別, (鏈接) B類, (鏈接) C類,等等,您可能也有興趣在x和y的一系列文章,我ž產品,或(連結)的廣告在這個網站。 If you still can’t find what you’re looking for (link to) please use my contact form to let me know.如果您仍然無法找到您要找的內容(鏈接) ,請使用我的聯繫方式,讓我知道。

To see what I wrote as an example - just visit my “看看我寫作為一個例子-只需訪問我的“ search everything搜尋 ” page. “頁面。 Now that we’ve build a custom search page, let’s customize our search results!現在我們已經建立一個自定義的搜索頁,讓我們自定義我們的搜索結果! Before I customize any code at all - I’m going to install the “在我自訂任何守則在所有-我要安裝“ Search Everything Wordpress Plugin 搜索所有WordPress的插件 “. “ 。 Normally wordpress only searches titles and the content of posts.通常情況下WordPress的唯一搜索標題和內容的職位。 But by installing search everything searches can also be configured to look at tags, comments, category names, excerpts, attachments, custom fields, and eve drafts!但是,通過安裝搜尋搜尋也可以配置看標籤,評論,分類名稱,摘錄,附件,自定義字段,並前夕匯票! It can also be used to “exclude” from search any category or post or page ID.它也可以被用來“排除”從搜索任何類別或張貼或網頁編號。

搜尋選項

Next, in your dashboard go to “Presentation -> Theme Editor -> Search Results (or search.php)”.下一步,在您的儀表板轉到“演示-> ”主題編輯器- >“搜索結果(或s e arch.php)” 。 This is your “search results template”.這是你的“搜索結果模板” 。 Any changes you make to this file will customize the way your search results come out any time a search is performed on your blog, whether from your new search page we created earlier, or from the search box on any page of your blog.您所做的任何變更到本文件將自訂的方式在搜索結果出來隨時執行搜尋在你的博客,無論是從您的新的搜索頁,我們以前創建,或從搜索框在任何網頁中的博客。

The search results as has a loop, and the way “the loop” works for search results is the same as any other Wordpress templated page.在搜索結果有一個循環,以及如何“循環”工程的搜索結果是一樣的其他任何WordPress的模板頁面。 It will show up to 10 results, and then next and previous links to get the rest of the results.它會顯示多達10個結果,然後下次和以前的聯繫得到其餘的結果。 In the case of search, it will show your most recent posts matching the keywords searched first, and then the older ones - which will (from the first search result page) only show a rediculous “<< previous entries" link. Some users not used to blogs or Wordpress will assume there are no more search results other than they see on the first page of results. I want some "google style" navigation for the search results that most people are used to seeing to make my search results more usable. Once again - we're going to use a Lester Chan plugin to get this done,在搜索,它會顯示您最近的職位相匹配的關鍵字,然後在舊的-這將(從第一個搜索結果頁)只顯示一個荒謬的“ ” “以前的作品”鏈接。一些用戶不用於WordPress的博客或將承擔沒有更多的搜索結果以外,他們看到的第一頁的結果。我想一些“谷歌式”導航的搜索結果中,大多數人都習以為常,使我國的搜索結果更可用。再次-我們要使用插件萊斯特成龍獲得這一工作要做, WP-PageNavi for Page Navigation可濕性粉劑, PageNavi的頁面導航 .

Again, every theme is different, and my theme has next and previous navigation before and after “the loop” in my search.php template.同樣,每一個主題是不同的,我的下一個主題和以往的航行前,後“環”在我search.php範本。 I’m going to remove it and replace with the WP-PageNavi wordpress plugin’s navigation.我要刪除和替換的渦噴PageNavi WordPress的插件的導航。 First I find the current navigation, which in my template looks like this:首先,我認為目前的導航,這在我的模板看起來像這樣:

 <div class="navigation"> <div class="alignleft"><?php next_posts_link('&laquo; Previous Entries') ?></div> <div class="alignright"><?php previous_posts_link('Next Entries &raquo;') ?></div> <div style="clear:both;"></div> </div> 

Yours may look different, look for the lines with “Previous Entries” and “Next Entries”.你可能看起來不同,尋找線“前項”和“下一步作品” 。 I’m going to replace it with the plugin’s code for navigation:我要去取代它的插件的代碼導航:

 <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?> 

I made that change at both the top and bottom of my search.php because I want to have navigation before and after my search results pages.我的這種變化在頂部和底部search.php我,因為我想擁有導航之前和之後我的搜索結果頁上。 I wish google did!我想谷歌沒有! Now that I have decent navigation it’s time to do a little monetization of the search results.現在,我有像樣的導航的時候做一些貨幣的搜索結果。 One thing I didn’t mention previously was that when we created the custom search template earlier, I did place an ad in the bottom of that page after “the loop” for my blog network.有一件事我不提以前,當我們創建了自定義搜索模板前,我做了一個廣告的底部後,該網頁的“循環”為我的博客網絡。 However, that ad will only display on the “search page” we created.然而,該廣告將只顯示在“搜索頁”我們創建。 To have any kinds of ads display in our search results themselves, we have to place them in the “search.php” as well.有任何類型的廣告顯示在我們的搜索結果本身,我們已經把他們在“ search.php ”以及。 I’m going to place one google adsense block above the search results, and one blog network ad at the end of the search results.我要去的地方之一谷歌的AdSense塊以上的搜索結果中,和一個博客網絡廣告的結尾處的搜索結果。

I’m also going to place some adsense blocks interspersed throughout the search results, and to do that I’m going to place the following code within “the loop” of search.php:我也將發生某些AdSense塊穿插在整個搜索結果,並做到這一點我要去的地方下面的代碼在“循環”的search.php :

 <?php if ($count==2) { include('search-adsense-inline.php'); } ?> <?php if ($count==6) { include('search-adsense-inline.php'); } ?> <?php $count = $count + 1; ?> 

Let me explain what’s going on here.讓我解釋發生了什麼事情這裡。 I logged into adsense and grabbed some code for an adblock and I pasted that code into a new file I named “search-adsense-inline.php”.本人登錄到AdSense和抓住一些代碼和我的AdBlock代碼粘貼到一個新的文件I名為“搜索的AdSense inline.php ” 。 I uploaded that file to my theme directory.我上傳文件到我的主題目錄。 Then I placed the code above in my loop in search.php.然後,我把以上代碼在我的環search.php 。 The first lilne says to grab the contents of that file and place the ad after the second post, and the next line says to do it again after the sixth post.第一lilne說,抓住內容的文件和地點的廣告後的第二個員額,以及下一行說,這樣做後再次第六職務。 Google now lets you have 3 adblocks (a 3 text link blocks) per page, so with the first block before my results and then these two, that’s the max (adsense) ads I can place here.谷歌現在您可以有3個adblocks ( 3文本鏈接塊)每個網頁,以便與第一塊之前我的結果,然後這兩個,這是最大(的AdSense )的廣告我可以在這裡。 The third line sets up a counter that starts with the first post, and ads one for every post from 2-10 so it knows where to place your ads.第三行建立了一個櫃檯開始的第一篇文章和廣告,每一個員額從2-10所以知道把您的廣告。

This is entirely flexible, meaning you could ad more ads (that weren’t adsense) by creating other files and including them after other posts.這完全是靈活的,這意味著你可以更多廣告的廣告(即沒有的AdSense )通過建立的其他檔案,包括他們在其他職位。 You could not use adsense at all, and advertise or promote something else!你不能使用AdSense所有,並刊登廣告或宣傳別的! They don’t even have to be ads, they can be links to your content, or funny and weird sayings to freak people out.他們甚至沒有將廣告,它們可以鏈接到您的內容,或有趣,怪異反常的話來人。 ANYTHING you put in those files will be after the posts you choose, whether it’sa graphic, text, or code.什麼你把這些文件將在您選擇的職位,無論是圖形,文字或代碼。 Use your imagination - I just gave your a bazillion ways to now monetize your search results!使用你的想像力-我只給你一b azillion如何賺錢現在您的搜索結果!

If that weren’t enough - I have yet another way, you can如果這還不夠-我還沒有另一種方式,您可以 Monetize your search results with a datafeed 賺錢您的搜索結果與datafeed as well, which will grab products from merchants online using your affiliate feed and show those results on your search results pages.以及,這將吸引商家的產品線上使用您的下屬飼料和展示這些成果的搜索結果頁上。 The great thing about that hack is the fact that you never have a page with a blank search result again.偉大的事是黑客的事實是,你永遠不會有一個網頁,一個空白的搜索結果了。 If someone searches for “Wordpress Help” and you have no posts, an Amazon feed for instance would show books, or other products related to “Wordpress Help”.如果有人搜索“ WordPress的幫助” ,你有沒有職位,亞馬遜的飼料例如將顯示書籍,或其他產品有關“ WordPress的幫助” 。 While I have your attention on search, I may as well tell you about another great search plugin雖然我已請你注意搜索,我可能也告訴你另一個偉大的搜索插件 Psychic Search心理搜索 , which saves information on all search performed on your Wordpress blog. ,它可以對所有信息進行搜索您的WordPress的博客。 You can get reports and see what people have been looking for over the last week, or month, and beyond.你可以看到報告和人們一直在尋找在過去一周或一個月內,並超出。 It has reports on what they found, and which searches had no results at all.據報告,他們發現,其中的搜索沒有結果。 This is very, very helpful information to know - and possibly information you can use to create future posts!這是非常,非常有用的信息知道-信息和可能可以用來創造未來的職位!

I hope you learned something today, and now you should have a brand new search page and monetized pimped out search results!我希望你今天學到的東西,現在你應該有一個全新的搜索頁和貨幣pimped的搜索結果! If you have something to add or a question, as always - please comment now!如果您有什麼新增或一個問題,一如既往地-現在,請評論!


Tags:標籤: , , , ,

2 Responses to “Wordpress Hack #3: Custom Search Results Page and Template”第2反應“ WordPress的哈克# 3 :自定義搜索結果頁面和模板”

  1. Ramayadi Ramayadi Has the following to say...有下列說...

    Hi,您好,

    Thank you for share this, it really increase my knowledge on wordpress謝謝你分享這一點,真正提高我國知識WordPress的 = 00
    Ramayadi’s last blog post.. Ramayadi最後的博客帖子.. My Favorite Photographer 我最喜歡的攝影師

  2. REP 代表 Has the following to say...有下列說...

    Thanks for the very useful article.謝謝了非常有用的文章。 I’m going to modify the search page templates for my sites in this manner.我要修改搜索頁面模板為我的網站用這種方式。 A trifle OT, but I agree that Google Custom Search is not the way to provide search for one’s own site, but I do think that a Google custom search engine, monetized with Adsense, can be a nice addition to a site.小事加時賽,但我同意谷歌自定義搜索不是方式提供搜索自己的網站,但我不認為谷歌自定義搜索引擎,貨幣與AdSense ,可以是一個很好除了一個網站。 If nothing else, it helps earn a little bit of money from the “bounce” traffic.如果沒有別的,它有助於賺了一點錢的“反彈”的流量。 And I found that registering some custom search engines in a few places earned me a few back links.而且我發現,登記的一些自定義搜索引擎在一些地方獲得了我幾回的聯繫。 And, among other things, by having a search engine that people can add to the Google toolbar, and/or add to search box in the upper righthand corner of their browser can bring them back to the site for return visits.而且,除其他事項外,有一個搜索引擎,人們可以添加到谷歌工具欄,和/或添加到搜索框的右上角,其瀏覽器可以促使他們重新回到網站的回訪。

Question or Comment??問題或意見? ? Spill it Now...溢油現在...

跳躍的喜悅以上的評論!

We Reward Comments!我們獎勵的意見!


We dofollow links, and get your latest blog post as a byline under every new comment from the "CommentLuv" plugin!我們dofollow聯繫,並獲得最新的博客帖子的署名下的每一個新的評論從“ CommentLuv ”插件! Top commenters for every month are listed on every page of this site in a sidebar widget linked back to your URL!頂端commenters每個月列出的每個網頁上本網站的聯繫欄部件回到您的網址! We would like to reward you for becoming part of our community!我們要獎勵你成為我們的社會! Your comment is valuable not only to us, but also all the other readers of this blog!您的評論是有價值的,不僅給我們,而且所有的其他讀者的博客!


Click to add smilies to your post! 按一下以新增的笑臉到您的帖子! == [ ]^ = (= ( (= ( |= )研究= | 8= 0= ) 〜= 00= (= ; ;= ) ]= ; ; ;