Wordpress Hack #18: Hacking Wordpress Template Files WordPress的哈克# 18 :黑客WordPress的模板文件
3,824 views - 3824意見者:管Posted in: 張貼於:
3,824 views 3824意見
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.
This tutorial will teach you the essentials about wordpress template files and their hierarchy when hacking your theme . 本教程將教你最基本的有關WordPress的模板文件和他們的等級時,黑客的主題。
This post is part of這個職位的一部分 30 Wordpress hacks in 30 days 30 WordPress的黑客在30天內 . 。
In my last Wordpress Hack我在去年的WordPress的哈克 Templates for Alternate Sidebar Layouts模板候補欄佈局 I touched on how you can create a theme template file for a page that has different sidebars for the rest of your blog.老子談到如何您可以創建一個模板文件主題的網頁,有不同的sidebars的其餘您的博客。 Answering some questions and comments for that article showed me that I need to write a more in-depth article about how the Wordpress Template Hierarchy Works and the choices you have available to your when you create custom template files. This article should change the way you think about your Wordpress theme FOREVER !在回答一些問題和意見,這一條讓我明白,我需要編寫一個更深入的一篇關於如何WordPress的模板工程和層次的選擇,你提供給您當您創建自定義模板文件。 本條應改變你想想你的WordPress的主題永遠 !
Wordpress is a blogging system that has greatly evolved over. WordPress的是一個博客系統,極大地演變。 It’s evolved as much as bloggers and blogging itself has.它的演變多達博客和博客本身。 Now, you can use it as a full fledged “Content Management System”, you can even use it to create a static web site with no blog at all.現在,您可以使用它作為一個正式的“內容管理系統” ,你甚至可以用它來創建一個靜態的網站沒有博客在所有。 I would say that most of us use it as a blog, where the home page has a running “list of posts” from the newest to oldest.我要說的是,我們大多數人把它作為一個博客,在首頁有一個運行“列表中的職位”從最新到最古老的。 You can add those posts to “categories”, and now with Wordpress 2.3x+ you can also “tag” them.您可以添加這些職位的“類別” ,現在WordPress的2.3倍+您也可以“標籤”他們。
I’m going to explain to your how Wordpress works “out of the box”.我要解釋你如何WordPress的作品“出箱” 。 Keep in mind that this blog has been heavily customized, and I don’t even have blog posts on my homepage anymore - I’ve moved them to “/blog” on this site (which is something you can change in your Wordpress dashboard under “Settings”).請記住,此博客已定制,我甚至沒有博客文章,在我的主頁上了-我已經搬到他們“ /博客”在這個網站(這是你可以改變你的W ordPress的儀表板下“設置” ) 。
When a web browser visits a Wordpress blog, the software looks in your theme directory (/wp-content/theme) for the assigned theme.當一個網頁瀏覽器訪問一個WordPress的博客,該軟件尋找在你的主題目錄( / wp-content/theme )為指定的主題。 It’s either “default” (out of the box), or a custom one that you picked out and uploaded to your theme directory.這是不是“默認” (退出框) ,或自訂一個您挑選並上傳到你的主題目錄。 Wordpress then iterates through your theme files looking for which one to use in a specific order. WordPress的遍歷然後通過您的主題檔案尋找其中使用在特定的秩序。 That search order might change depending on what Wordpress has been asked to do (show the homepage, show a category page, show search results, show an archive page, etc.).為了搜尋可能改變取決於WordPress的已被要求做(顯示在首頁,顯示出類別頁面,顯示搜索結果,顯示存檔頁,等等) 。 Your understanding of this search order (called the “template hierarchy”) is vitally important to your understanding of how Wordpress Themes work.您理解這個搜索命令(稱為“模板等級” )是極其重要的理解如何WordPress的主題工作。 If you understand the template hierarchy in Wordpress, you can build custom theme template files and make your blog 100 times more effective, indexable (by search engines), and in the end more profitable.如果您了解模板等級WordPress的,您可以建立自訂主題模板文件,使您的博客100倍,更有效,索引(搜索引擎) ,並在年底更加有利可圖。
The那個 Wordpress Codex has an image of the template hierarchy WordPress的法典有一個形象的模板體系 that spells it all out.這說明這一切了。 Since that is too big for this page, I created a custom one that is a bit more simplistic for display here:由於這是太大的這個頁面,我創建了一個自定義之一,這是更簡單的顯示位置:

When I said “simplistic”, I really meant it - didn’t I?當我說: “簡單” ,我真的意味著-沒有我? As the pictures shows, when a web browser calls a site with Wordpress installed it asks itself “which (template) page do I need (to show the content)?” I’ll explain these php files in depth in a few minutes, but first generally Wordpress asks, is it a date.php (archive) page, is it a category page, is it a tag page, is it the “home” (or blog) page, is it a single (post) page, is it a Wordpress “page” (that you wrote), is it an error 404 (page not found) or seach page, and last is it some kind of attachment.正如圖片顯示,當一個網頁瀏覽器要求網站與WordPress的安裝它要求自己“這(模板頁)我需要(顯示的內容) ? ”我會解釋這些PHP文件中深入幾分鐘,但一般WordPress的第一個要求,是一個date.php (存檔)頁,是一類網頁,是一個標籤頁,它的“家” (或博客)網頁,是一個單一的(後)一頁,是它WordPress的“頁” (即你寫) ,是一個錯誤404 (找不到網頁)或搜索頁,最後是某種形式的附件。 You can see in the examples that if the right file is not found, with the exception of attachments, Wordpress will just default to the index.php to display what it needs.你可以看到的例子中,如果正確的檔案找不到,除附件, WordPress的將只是默認的index.php來顯示它的需要。
The Wordpress Home Page template or “home.php”在WordPress的主頁模板或“ home.php ”
Now, let’s dig a little deeper - shall we?現在,讓我們挖一個小更深-我們應? Let’s say that you want to customize your “home” page of your Wordpress blog - the one that by default shows your blog posts.比方說,您要自訂您的“家”網頁中的WordPress的博客-即默認顯示您的博客帖子。 MOST themes I’ve dealt with just use “index.php” to show your home page content.大多數主題我已經處理只需使用“的index.php ”顯示您的網頁內容。 But if you look at the template hierarchy you see that the first page Wordpress looks for (for the home page) is “home.php”.但如果你看看模板等級你看到的第一頁WordPress的尋找(為主頁)是“ home.php ” 。 Your theme may or may not contain this file.您的主題可能會或不會包含這個文件。 If you don’t have one, just open index.php and “save as” home.php - customize it to your liking and now you have a custom theme home page template file.如果你沒有一個公正公開的index.php和“另存為” home.php -定制,以您喜歡,現在您有一個自定義主題的主頁模板文件。 If you want your blog posts to be somewhere else other than your “home” page you will need to create a different template file for a “page” (instructions below), and then assign that “page” the proper template in “Manage->Pages-Pagename” in your Wordpress dashboard.如果您希望您的博客帖子是別的地方比您的其他“家”網頁中,您將需要建立一個不同的模板文件為“網頁” (以下說明) ,然後指定的“頁”適當的模板“管理“網頁,網頁”在您的WordPress的儀表板。 Then delete “home.php” and in your Wordpress dashboard “Settings”, assign a different page to home to display what you want.然後刪除“ home.php ” ,並在您的WordPress的儀表板“設置” ,指定一個不同的頁面家,以顯示就是你想要的。
You should also read你也應該閱讀 Wordpress Hack #1: Query Posts WordPress的哈克# 1 :查詢職位 , and ,並 Wordpress Hack #15: 12 Ways to Hack Your Home Page WordPress的哈克# 15 : 12途徑哈克您的首頁 . 。
Wordpress Category Template Pages WordPress的類網頁模板
When you write a post you can assign it to a “category”, which is one way of organizing your content.當您撰寫文章您可以指派到一個“類別” ,這是一個的方式來組織您的內容。 It’s also a way for your visitors to filter through your past content.這也是一個途徑訪問者通過過濾您過去的內容。 When your categories are listed in your sidebar, and you click on one, Wordpress will filter and show only posts that were assigned to that category.當您的類別中列出您的欄,你點擊其中一個,將WordPress的過濾和顯示員額只被分配到這一類。 This is very handy.這是非常方便的。 Wordpress has two ways it can show you post content on pages like this, one is an “excerpt” and the other is the full post. WordPress的有兩種方式可以顯示您張貼內容的網頁上這樣,一個是“摘錄” ,另一個是充分職務。 Either way - you have some of the same content listed on this page as you do your (blog) home page, your archive (past post) pages, and (if you’re using them) also tag pages.無論哪種方式-你有一些相同的內容列在這個網頁你做你的(博客)的主頁,您的存檔(過去後)的網頁,以及(如果你使用他們)也標記的網頁。 This is a horrible, horrible thing to google as they see “duplicate content”.這是一個可怕,可怕的事情,以谷歌為他們看到“重複的內容” 。 That means that all your pages (while useful to visitors) like this probably won’t be indexed by google and may even count against you.這就是說,您所有的網頁(而有益的訪客)這樣可能將不會被索引的谷歌和計數,甚至可能對你。
Now, when it comes to google, and search engines in general - the object is to get as many pages as you can indexed.現在,當涉及到谷歌和搜索引擎的一般-的目標是讓盡量多的網頁,你可以索引。 The more pages indexed, the more chances you have of somebody finding your blog.該索引的網頁越多,就越有機會,你的人找到您的博客。 Most themes that I’ve seen don’t have any category page at all and just index.php is used to display category content.大多數的主題,我看到沒有任何類別頁面上所有和公正的index.php是用來顯示類的內容。 This is ok as a category.php file would be used for every single category you have added content too.這是玉作為category.php檔案將被用於每一個類別中添加的內容太多。 If you want “original content” on each and every category page then you will need to create a template file for each and every catetgory you have created (I hope you haven’t gone wild).如果你想“原創內容”每個類別頁面那麼您將需要創建一個模板文件為每個catetgory您已創建(我希望你有沒有野生) 。
If your Wordpress dashboard go to “Manage->Categories”.如果你的WordPress的儀表板轉到“管理”顧客“ 。 If you put your mouse over any category name (without clicking), in the bottom of your web browser in the status bar will appear the “category ID”.如果你把鼠標放在任何一類的名稱(不點擊) ,在底部的網頁瀏覽器在狀態欄中會出現的“類別編號” 。

If your theme doesn’t have a “category.php” file (if it does - use it), open up your “index.php” file and “save as “category-18.php” (using your category ID of course), and create one category template file for each category you have in your blog.如果您的主題不會有一個“ category.php ”文件(如果它-使用它) ,打開你的“的i ndex.php”文件和“另存為”類別1 8.php“ (使用您的類別編號當然) ,並創建一個模板文件類對每一類您在您的博客。 In each file add a few paragraphs of original content to each category file, and then when search engines come around to index you - you’ll have some original content on each and every category page!!在每個文件添加幾段原創內容對每一類檔案,然後當搜索引擎來索引左右你-你有一些原始內容,每個類別頁面!
Also, see this as an opportunity (not work).此外,認為這是一個機會(不工作) 。 Why?為什麼? Because it’s an opportunity to make more money.因為這是一個機會,使更多的錢。 You can place different relevant targeted niche ads on each and every category page!您可以將不同的相關針對性的特殊廣告每個類別頁面! Look at home many additional monetization opportunities you just created!在家裡看許多額外的贏利機會您剛剛創建!
Regarding Category Templates, you should also read關於類模板,你也應該閱讀 Wordpress Theme Hack: Show Category Images WordPress的主題破解:圖像顯示類 , and ,並 Wordpress Hack: 9 Things You can do with Categories WordPress的哈克: 9事情可以做的分類 . 。
Wordpress Tag Template Pages WordPress的標記模板頁
Wordpress Template Tag pages work the exact same way as category template pages, with the exception that they use the “slug” (tag keywords) instead of the ID (I don’t know why category template can’t do this too!). WordPress的模板標記網頁的確切工作一樣類模板網頁,除了他們使用的“塞” (標記關鍵字) ,而不是身份證(我不知道為什麼類模板無法做到這一點呢! ) 。 So, if you tagged a post “wordpress hacks”, then your tag template file would be “tag-wordpress-hacks.php”.因此,如果您標記後“ WordPress的黑客” ,那麼您的標記模板文件將是“標籤WordPress的, hacks.php ” 。 In the organization of a blog, categories should be very broad, and tags should be descriptive.在該組織的一個博客,分類應該非常廣泛,標籤應描述。 A good example of a category would be “news”, or “code”.一個很好的例子,一類是“新聞”或“代碼” 。 Tags could be “9-11″ or “css web design”.標籤可以是“ 9-11 ”或“的CSS網頁設計” 。 That’s why optimally you should have maybe a dozen or two categories, but you might have hundreds (even thousands!) or tags.這就是為什麼你應該最有可能是十幾或兩個類別,但您可能有數以百計(甚至上千! )或標記。 Since I’ve had the same categories since this blog started, but I add new tags all the time - I probably won’t create a custom tag template page for every tag I have.既然我已經在同一類別,因為這博客開始,但我添加新的標籤所有時間-我可能不會創建一個自定義標籤模板頁的每一個標記我。 I WILL, however, create one for all the tags I feel are most-used, most-important, and most-monetizable!然而,我將創建一個所有的標籤我覺得是最常用,最重要的,最為貨幣!
You should also read你也應該閱讀 Wordpress Hack #15: Hacking Wordpress Tags WordPress的哈克# 15 : WordPress的標記亂砍 . 。
Wordpress Author Template Pages WordPress的作者網頁模板
This is a template page I seldom see used, because most blogs have one author.這是一個模板頁我很少見用,因為大多數博客作者之一。 Many blogs though have multiple contributors and authors - and each post can have an “authored by” link which takes them to the author page.許多Blog儘管有多個貢獻者和作者-和每個職位可以有一個“作者”鏈接,其中考慮它們的作者頁。 If “author.php” doesn’t exist, index.php will be used by default.如果“ author.php ”並不存在,的index.php將被默認。
The author.php page can list pages written by that author, all authors, it can list the authors - and be customize any number of ways.該author.php頁可以列出網頁撰寫的作者,所有作者,它可以列出的作者-和自定義任何數量的方式。 As I said, most themes don’t have this - but if you want a blog with multiple authors you probably want to create a custom author page.正如我所說的,大部分的主題沒有這一點-但如果你想要一個博客作者多你可能想創建一個自定義的作者頁。 For more information on what’s possible read the欲了解更多有關什麼可能讀 Wordpress Codex page on Author Templates. WordPress的法典上作者的範本。
Wordpress Single Post Page Templates WordPress的單頁模板後
When you write a post in Wordpress, it’s gets listed on your home (blog) page, your archive page, and the appropriate category and tag pages.當您撰寫文章在WordPress的,它得到列在您的家(博客)網頁,您的存檔頁,以及相應類別和標籤的網頁。 But is also has it’s own (full) page, which is what we call the “single.php” or single post page.但也有它自己的(全)網頁,這正是我們稱為“ single.php ”或單一的帖子頁。 Nearly every theme has a single.php file, but if it doesn’t then (of course) the single post is displayed using the default index.php theme file.幾乎每一個主題有一個single.php文件,但如果它不然後(當然)單後顯示使用預設的index.php主題文件。
If your theme doesn’t have a “single.php” create one (based on index.php), if it does - consider customizing it.如果您的主題不會有一個“ single.php ”創建一個(根據的index.php ) ,如果這樣做-考慮定制它。 If you use Adsense I think that the single post is a great place to add a 336×280 block just before your content.如果您使用的Adsense我認為,單一的工作是一個很棒的地方,新增一個336 × 280塊剛剛收到您的內容。
Wordpress “Page” Templates WordPress的“頁”模板
I think that a Wordpress “Page” is the most mis-understood thing in the entire blogging software.我認為WordPress的“頁面”是最錯誤的理解的事情在整個博客軟件。 When you go to “Write” in the Wordpress dashboard you have options to write a “Post” or a “Page”.如果你去“寫入”在WordPress的儀表盤你選擇寫“郵報”或“頁” 。 The choice to write one or the other is a choice basically to use the “blogging paradigm” or not.選擇寫一方或另一方是一種選擇基本上是使用“博客模式”與否。 By writing a “post” you are writing not only a “single” page post, but it also will be listed on the home (blog) page, and archive page, as well as category and post pages.寫一個“後”你是寫作不僅是一個“單一”頁面後,但它也將被列為家(博客)網頁,存檔頁,以及類別和帖子頁。 The choice to author a “page” is like an “about page” - it’s static in the sense that it won’t be listed on blog type pages.選擇作者的“頁”就像是一個“對頁” -這是靜態的,因為它不會被列在博客類型的網頁。 It will only be listed in the navigation of your theme or a list of “pages” in the sidebar.它只會列在你的導航或主題的名單“網頁”中的側欄。
Pages can be anything, an about page, an advertise with us page, a contact page, or even a series page where you provide descriptions of posts and link to them on one central location.網頁可以是任何一個對頁,一個與我們宣傳網頁,網頁的聯絡,甚至是一系列頁面,您提供的職位說明和鏈接,他們就一個中心位置。 Pages are a great way to organize content in your blog, and the more you create - the more you’ll find the need to create custom layouts.網頁是一個偉大的方式組織內容在您的博客,並創造更多的你-更為您會發現需要創建自定義佈局。 On this blog I created a page template with a custom sidebar for my在這個博客我創建了一個網頁模板自定義欄我 forum論壇 . 。 I also created a special one for my我還設立了一個特殊的人對我 archived posts sitemap存檔帖子網站 , and ,並 testimonials推薦 . 。 On one of my other blogs I created one with no sidebar at all for my關於我的其他blog我創建一欄沒有在所有為我 find cheapest gas找到最便宜的加油站 page.網頁。
If you want to create a custom template theme file for that you can assign to any “page” that you write in Wordpress all you need to do is open your index.php file and “save as” whatever name you want (like sitemap-template.php or whatever).如果你想創建一個自定義模板文件的主題,您可以指派給任何“網頁” ,你寫在WordPress的所有您需要做的就是打開你的index.php文件和“保存為”無論你想的名字(如網站, template.php或任何) 。 I try to name all mine something-template.php, just to keep them straight.我嘗試所有地雷的名字的東西, template.php ,只是為了讓他們直。
In the top of that page you are going to need to add the following code (copy and paste) so it gets identified as a template in Wordpress:在最重要的是網頁中,您將需要添加以下代碼(複製和粘貼) ,所以它得到確定為模板WordPress的:
<?php /* Template Name: My Template Page */ ?> Change “My Template Page” to whatever you’re naming your template, and then customize the page by creating a更改“我的模板頁”的任何您命名您的模板,然後定制的網頁建立一個 master archive index template主存檔索引模板 , create a創建一個 custom 404 error page template自定義404錯誤頁模板 , or even ,甚至 create templates with alternate sidebar layouts創建模板候補欄佈局 . 。
In your Wordpress dashboard to assign a template, just scroll down past the post text box and under “template”, just find your custom template in the drop down and choose it to apply that particular template to your page and “save”!在你的WordPress的儀表板,指派一個模板,只需向下滾動過去後文本框和“模板” ,只要找到你自定義模板中的下拉,並選擇它適用於特定的範本到您的網頁和“拯救” !
The possibilities are endles…的可能性endles ...
Creating a Custom Wordpress Search Page創建自定義WordPress的搜索頁
In Wordpress when someone searches your content, “search.php” is used for the results.在WordPress的當某人搜尋您的內容, “ search.php ”是用於結果。 You could create a你可以創建一個 custom search results template自定義搜索結果模板 so people find what they need each and every time.因此人們找到他們所需要的每一個時間。 You could also monetize this page with an ad if you wished, or just provide links to your top content on top of the search results.您也可以賺錢這個網頁與廣告如果你想,或者只是提供鏈接到您的頂端上的內容上方的搜索結果。 If your theme doesn’t have a search.php, just create one based on index.php.如果您的主題沒有search.php ,只需建立一個基礎上的index.php 。
Creating a Custom 404 Error Page in Wordpress創建自定義404錯誤在WordPress的頁面
I think that one of the most important things is not just what people see when they find your content, it’s what they see what they don’t find anything at all.我認為,一個最重要的事情不僅是人們看到當他們找到您的內容,這是他們看到他們沒有找到任何東西。 If someone gets a “404 page not found” error on your Wordpress blog, Wordpress looks for the “404.php” file first, and if you don’t have one it uses index.php by default.如果有人獲得“ 404找不到網頁”的錯誤在您的WordPress的博客, WordPress的期待的“ 404.php ”文件第一,如果你沒有使用一個默認的index.php 。 If you don’t have one, create it based on your index.php file - and then customize it right away!如果您沒有一個,建立基於您的index.php文件-然後自定義問題! Most themes just have some bogus default message like “error - not found”.大多數的主題只是有一些虛假默認郵件,如“錯誤-未找到” 。
The first thing you want ot do is to erase that and put a nicer message.的第一件事,你想加時賽做的是清除,並把好消息。 Write a paragraph and include some links to your top areas AND your contact form in case they want to get ahold of you.收件一個段落,其中包括一些鏈接到您的頂端領域和您的聯繫方式的情況下,他們希望能阿霍爾德你。 Next, install a plugin that gives them automatically the best results it can - for details read下一步,安裝一個插件,讓它們自動最好的結果可以-閱讀詳情 Wordpress Hack #4: Create a Custom Error 404 Page WordPress的哈克# 4 :創建自定義錯誤404頁 . 。
Wrapping it Up…環繞它漲...
No matter where you got your Wordpress theme, and whether it was basic or a Premium one you paid top dollar for - it CAN and SHOULD be customized to it’s fullest possible extent.無論你有你的WordPress的主題,無論是基本或高級你付出的頂部美元-它可以而且應該進行定制,這是盡可能充分。 A theme designer often is worried about the web design and layout aspect ONLY with no consideration for organization of your blog, it’s content, SEO and search indexing capabilities - let alone error messages or search pages.主題設計師,常常是擔心網頁設計和佈局方面不僅沒有考慮組織您的博客,它的內容,搜索引擎優化和搜索索引的能力-更不用說錯誤訊息或搜尋網頁。 Do your self (and your visitors) a favor and make sure that you have ALL areas of your Wordpress template hierarchy covered!你的自我(和您的訪客)一個忙,並確保你有所有領域的WordPress的模板體系覆蓋!
I hope I didn’t give you too much work to do, but by the time you’re done with everything I just taught you you will have a much better understanding of how Wordpress and Wordpress Themes work.我希望我沒有給你太多的工作要做,但時間你做的一切我只想告訴你,你將有一個更好的了解如何WordPress的主題和WordPress的工作。 When you’re done you might be interested in moving on an reading當您完成您可能有興趣的轉移閱讀 How to Create Your Own Wordpress Theme如何創建自己的WordPress的主題
Tags:標籤: tutorial教程 , , wordpress-hacks WordPress的,黑客 , , wordpress-template WordPress的模板




















July 30th, 2008 at 8:20 pm 2008年七月30號在下午8點20分
All of the wordpress sites I’ve built - from scraaaaaaatch, at that - and I’ve never thought of creating an author.php page!所有的WordPress的網站我已經建立-從s craaaaaaatch,在這-我從來沒有想到創造一個au thor.php頁面! Ugh!啊呀! I had to write that one down.我不得不寫,一個下來。 LOL.大聲笑。
Erikas last blog post.. Erikas最後的博客帖子.. Miami Web Design邁阿密網頁設計
August 10th, 2008 at 8:21 am 2008年八月十日在上午08時21分
Thanks for sharing!感謝您分享!
I’ve been doing some tweaking on Blogger template before but I find i hard on Wordpress template.我一直在做一些調整的Blogger模板,但我很難找到字母i在WordPress的模板。
Paul Us last blog post..保羅我們最後的博客帖子.. Courage + money勇氣+金錢
success [Digg]成功[ Digg的]