站长百科 | 数字化技能提升教程 数字化时代生存宝典
首页
数字化百科
电子书
建站程序
开发
服务器
办公软件
开发教程
服务器教程
软件使用教程
运营教程
热门电子书
WordPress教程
宝塔面板教程
CSS教程
Shopify教程
导航
程序频道
推广频道
网赚频道
人物频道
网站程序
网页制作
云计算
服务器
CMS
论坛
网店
虚拟主机
cPanel
网址导航
WIKI使用导航
WIKI首页
最新资讯
网站程序
站长人物
页面分类
使用帮助
编辑测试
创建条目
网站地图
站长百科导航
站长百科
主机侦探
IDCtalk云说
跨境电商导航
WordPress啦
站长专题
网站推广
网站程序
网站赚钱
虚拟主机
cPanel
网址导航专题
云计算
微博营销
虚拟主机管理系统
开放平台
WIKI程序与应用
美国十大主机
编辑“
WordPress:Tag Templates
”
人物百科
|
营销百科
|
网赚百科
|
站长工具
|
网站程序
|
域名主机
|
互联网公司
|
分类索引
跳转至:
导航
、
搜索
警告:
您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您
登录
或
创建
一个账户,您的编辑将归属于您的用户名,且将享受其他好处。
反垃圾检查。
不要
加入这个!
= Introduction = = 介绍 = Since the advent of [[WordPress:Using Themes|Themes]] in WordPress 1.5, changing the look and feel of your WordPress site has become fairly straightforward. For instance, when a viewer clicks on a link to one of the Categories on your site, he or she is taken to a page listing the Posts from that particular Category in chronological order, from newest Posts at the top to oldest at the bottom. There are many display choices, including whether to display the complete post or post excerpts, and what additional information to display (title, author, publish date, last modified time, etc.). Each theme makes different choices, and you might want to change them. 自从WordPress1.5出现了[[WordPress:Using Themes|主题]],更改你的WordPress站点的外观和整体效果变得轻而易举。例如,当访客点击你的站点上其中的一个类别链接的时候,他或者她就会来到一个页面,这个页面以时间顺序列出了那个类别的所有文章,将最新发表的文章列在顶上方,将最迟发表的文章列在最下方。可以以许多不同的方式显示文章,包括是否显示完整的文章还是显示摘录,显示哪些额外信息(标题,作者,发表日期,最后更改的时间,等等)。每个主题会有许多不同的选择,你可能想要更改这些选项。 This article explains how to change what happens when the blog viewer is visiting one of your site's Category pages. This involves the use of Themes and Template files, so if you are new to template files, you might want to read [[WordPress:Using Themes]] and [[WordPress:Stepping Into Templates]] first. 这篇文章解释了,当博客访客浏览你的站点的其中一个类别页面的时候,怎样更改出现的变化。这包括使用主题和主题文件,因此,如果你刚刚接触模板文件,你可能想要先阅读[[WordPress:Using Themes|使用主题]]和 [[WordPress:Stepping Into Templates|走进模板]]。 == What Template File is Used? == == 使用什么模板文件? == The first step in modifying what happens when someone visits a Category page is to figure out which of your theme's files is going to be used to display the posts. This is known as the [[WordPress:Template Hierarchy]]. 当有人浏览类别页面的时候,更改发生变化的第一步是要了解,将要使用你的哪个主题文件来显示文章。这也称为[[WordPress:Template Hierarchy|模板层级]]。 In the case of categories, the hierarchy is fairly simple. For instance, suppose the ID number of the Category in question is '''6'''. The Template Hierarchy specifies that WordPress will use the ''first'' Template file it finds in your current Theme's directory from the following list: #<tt style="font-weight:bold; color:#036">category-6.php</tt> #<tt style="font-weight:bold; color:#036">category.php</tt> #<tt style="font-weight:bold; color:#036">archive.php</tt> #<tt style="font-weight:bold; color:#036">index.php</tt> 关于类别,层级非常简单。例如,假如讨论的类别ID的数字是'''6'''。模板层级规定WordPress将会使用WordPress在下面的列表,你的当前主题目录中找到的''第一个''模板文件: #<tt style="font-weight:bold; color:#036">category-6.php</tt> #<tt style="font-weight:bold; color:#036">category.php</tt> #<tt style="font-weight:bold; color:#036">archive.php</tt> #<tt style="font-weight:bold; color:#036">index.php</tt> That is, if you do not have a <tt style="font-weight:bold; color:#036">category-6.php</tt>, WordPress will check for a <tt style="font-weight:bold; color:#036">category.php</tt>, and so on. 也就是说,如果你没有<tt style="font-weight:bold; color:#036">category-6.php</tt>,WordPress就会查看<tt style="font-weight:bold; color:#036">category.php</tt>,等等。 So, if you want to make the Category whose ID number is 6 look different from what it is currently (and different from other Category pages), you would want to create a <tt>category-6.php</tt> file. If you want to make all Category pages look different from other archive pages (such as date and author archives), then you would want to create or modify the <tt>category.php file</tt>. If you want to make changes to the look of all archive pages, you can create or modify the <tt>archive.php</tt> file. And if you modify the <tt>index.php</tt> file, you will affect your entire blog. 因此,如果你希望类别ID是6的类别看起来与当前类别页面不同(也与其它类别页面不同),你可能想要创建<tt>category-6.php</tt>文件。如果你希望所有的类别页面与其它归档页面看起来不同(例如日期和作者归档),那么你可能想要创建或者更改<tt>category.php file</tt>。如果你想要更改所有的归档页面的外观,你可以创建或者更改<tt>archive.php</tt>文件。而且如果你更改了<tt>index.php</tt>文件,你就会影响你的整个博客。 If you need to create a new file, it is a good idea to copy it from the next file in the hierarchy that exists. For instance, if you want a special display for Category 6, begin by copying the <tt>category.php</tt> file, or if you don't have one, use <tt>archive.php</tt>, and so on. 如果你需要创建新的文件,最后从存在的层级中的另一个文件中复制这个文件。例如,如果你想要特别地显示类别6,首先复制<tt>category.php</tt>文件,或者如果你没有这个文件,请使用<tt>archive.php</tt>,等等。 = Examples = = 例子 = Now that you've figured out which template file in your theme's directory you need to modify, in order to make changes to the look of Category pages, let's look at some examples. In these examples, when it says "edit your template file", it means to edit the file you chose in the section above. 既然现在你已经了解需要更改你的主题目录中的哪个模板文件,为了更改类别页面的外观,让我们看看一些例子。在这些例子中,当显示"编辑你的模板文件"的时候,意思是编辑你从上面的部分中选择的文件。 == Adding Text to Category Pages == == 将文本添加到类别页面== === Static Text Above Posts === === 文章上方的静态文本=== Suppose you want some static text displayed before the list of Posts on your Category page(s). By "static", we mean text that remains the same, no matter which posts will be displayed below, and no matter which category is being displayed. Here is how to do it: above [[WordPress:The Loop]] section of your Template file, insert the following code: 假如你希望你的类别页面的文章列表之前显示一些静态文本。"静态",指的是,不管下面显示哪篇文章,文本都保持一致。下面是怎样显示静态文本的操作:在你的模板文件的[[WordPress:The Loop|The Loop]]部分的上面,插入以下的代码: <pre><nowiki> <p> This is some text that will display at the top of the Category page. </p></nowiki></pre> <pre><nowiki> <p> 这些文本将会显示在类别页面的顶上方。 </p></nowiki></pre> === Different Text on Some Category Pages === ===一些类别页面上的不同的文本 === A slightly more complex possibility is that you want different text to display depending on which category page the visitor is viewing. Then you would add the "default" text to the main <tt>category.php</tt> file, and create special <tt>category-#.php</tt> files (with their own version of the text, as described in the Introduction) for each category that needs special text at the top. 稍微复杂的情况是,根据访客浏览不同的类别页面,你希望显示不同的文本。那么你将要将"默认"文本添加到主要的<tt>category.php</tt>文件,并且为需要在顶上方出现特别文本的每个类别,创建特别的<tt>category-#.php</tt>文件(带有自己版本的文本,如介绍中所描述的)。 This does however create a lot of files in your theme directory, and can be avoided using the following code '''OUTSIDE''' the loop: 这样,在你的主题目录中创建了许多文件,而且可以避免在loop '''外'''使用下面的代码: <pre><nowiki> <?php if (is_category('Category A')) { ?> <p>This is the text to describe category A</p> <?php } elseif (is_category('Category B')) { ?> <p>This is the text to describe category B</p> <?php } else { ?> <p>This is some generic text to describe all other category pages, I could be left blank</p> <?php } ?> </nowiki></pre> <pre><nowiki> <?php if (is_category('Category A')) { ?> <p>这是描述类别A的文本</p> <?php } elseif (is_category('Category B')) { ?> <p>这是描述类别B的文本</p> <?php } else { ?> <p>这是描述所有其它类别页面的一般文本,我可以保留为空白 </p> <?php } ?> </nowiki></pre> This does the following. Checks to see if we are looking at Category A, if we are then show the first bit of text, but if we're not then check if we are looking at Category B. If we are then show that bit of text, and finally, if it is neither Category A or B, then show this default text. 这个代码执行以下的操作。查看我们是否在查找类别A,如果我们是在查找类别A,我们会显示一些文本,如果我们不是在查找类别A,代码就会查看我们是否在查找类别B。如果我们那时显示那点文本,而且最后,如果既不是类别A也不是类别B,那么就显示这个默认文本。 === Text Displaying Only on First Page of Archive === === 只在归档的第一个页面上显示文本=== Another thing that can happen is that if your Category contains more posts than will fit on one page (according to the Options for Blog Reading you have set in the Administration panels of your blog), the category archive will split into multiple pages. And maybe you only want to display your static text if the viewer is on the first page of results, or you want to display different text for the other pages. 另一种情况是,如果你的类别包含的文章数目超过了一个页面上可以包含的文章数目(根据你在你的博客的管理面板上设置的博客阅读选项),类别归档将会分为几个页面。如果访客在第一个页面上,你可能只想要显示你的静态文本,或者你想要为其它的页面显示不同的文本。 To make this happen, you can use a PHP <tt>if</tt> statement, which looks at the value of the <tt>$paged</tt> WordPress variable (<tt>$paged</tt> is equal to the page number: 1 for the first page of results, 2 for the second page, etc.). It may sound complicated, but it's actually not too bad. Just put the following above [[WordPress:The Loop]]: 使得这种情况发生,你可以使用PHP<tt>if</tt>声明,查看<tt>$paged</tt>WordPress变数的参数值(<tt>$paged</tt>与页面数字相等:1代表结果的第一个页面,2代表第二个页面,等等)。听起来复杂,但是事实上并不太难。只要将下面的代码放到[[WordPress:The Loop|The Loop]]的上面: <pre> <?php if ( $paged < 2 ) { ?> <p>Text for first page of Category archive.</p> <?php } else { ?> <p>Text for subsequent pages of Category. Can be left out.</p> <?php } ?> </pre> <pre> <?php if ( $paged < 2 ) { ?> <p>类别归档的第一个页面的文本。</p> <?php }其它的{ ?> <p>类别的其它页面的文本。可以留为空白。</p> <?php } ?> </pre> === Category Name === ===类别名=== Another possibility is to put the category name at the top of the page. If this is not already part of your template, you can add it by doing something like this, above [[WordPress:The Loop]]: <pre> <p>Category: <?php single_cat_title(); ?></p> </pre> 另一种情况是将类别名放到页面的顶上方。如果这还不是你的模板的一部分,你可以在[[WordPress:The Loop|The Loop]]上执行这个操作,将这个部分添加到你的模板上: <pre> <p>类别: <?php single_cat_title(); ?></p> </pre> == Modifying How Posts are Displayed == == 更改文章的显示方式 == === Excerpts vs. Full Posts === === 摘录 vs. 完整的文章 === Perhaps you are looking to cut down on the size of your Category pages. You could do this by displaying excerpts rather than the entire content of each Post. To do this, you will just need to find where it says <tt style="font-weight:bold; color:#036">[[WordPress:Template Tags/the_content|the_content()]]</tt> inside [[WordPress:The Loop]] in your Template, and replace it with <tt style="font-weight:bold; color:#036">[[WordPress:Template Tags/the_excerpt|the_excerpt()]]</tt>. These will most likely be inside PHP tags: <pre> <?php the_excerpt(); ?> and <?php the_content(); ?> </pre> 也许你想要更改你的类别页面的大小。显示摘录而不是每篇文章的整个内容,你就可以做到这一点。你只要在你的模板的[[WordPress:The Loop|The Loop]]内找到显示为<tt style="font-weight:bold; color:#036">[[WordPress:Template Tags/the_content|the_content()]]</tt>的位置,并将其更改为<tt style="font-weight:bold; color:#036">[[WordPress:Template Tags/the_excerpt|the_excerpt()]]</tt>,即可。这些很有可能在PHP标签的内部: <pre> <?php the_excerpt(); ?> 和 <?php the_content(); ?> </pre> Conversely, if your Theme is currently displaying excerpts and you want full posts, replace <tt>the_exerpt</tt> with <tt>the_content</tt>. 与此相反,如果你的主题当前显示摘录,但是你想要显示完整的文章,将<tt>the_exerpt</tt>替换为<tt>the_content</tt>。 === Display Images Linking to Full Posts === === 显示图像链接到完整的文章=== Another thing that is interesting to do in a category archive page is to replace the post content with an image that links to your post. To do this, you will first need to put images into the Excerpt sections of your posts. Here's how to do that, for one post: 处理类别归档页面的另一个有趣的操作是使用链接到你的文章的图像代替文章内容。你首先要将图像放置到你的文章的摘录部分。下面是怎样将图像放到一篇文章的摘录部分: # Upload an image on the post editing screen. # Switch to the "Code" editor, rather than the "Visual" editor. # Use the Uploads / Browse tab to insert the image into your post. Make sure to insert the full-sized image, with no link. # Copy the inserted HTML <tt>img</tt> tag, and paste it into the "Optional Excerpt" section of the post editing screen. # Finish writing the post content (you can remove the image), and publish the post. #将一个图像上传到文章编辑界面。 #切换到"Code"编辑器,而不是"Visual"编辑器。 #使用上传/浏览标签将图像插入到你的文章。确定插入了完整大小的图像,没有链接。 #复制已插入的HTML<tt>img</tt>标签,并且将标签粘贴到文章编辑界面的"可选择的摘录" 部分。 #结束编写文章内容(你可以移除图像),并且发表文章。 Now you will need to modify your template. We'll use a trick: the [[WordPress:Template Tags/the_excerpt_rss|the_excerpt_rss()]] Template Tag does not put a paragraph tag around the excerpt. So we can use it to insert the <tt>img</tt> HTML and put it inside a link. Here's what you need to put into your Template, in place of using <tt>the_content</tt>: 现在你需要更改你的模板。我们会使用一种方法:[[WordPress:Template Tags/the_excerpt_rss|the_excerpt_rss()]]。模板标签没有将段落标签放置在摘录周围。因为我们使用模板标签插入<tt>img</tt> HTML并且将其放入链接内容。下面是我们需要放入模板中,取代<tt>the_content</tt>的内容: <pre> <a href="<?php the_permalink() ?>"> <?php the_excerpt_rss(); ?> </a> </pre> <pre> <a href="<?php the_permalink() ?>"> <?php the_excerpt_rss(); ?> </a> </pre> Caveat: using the excerpt this way '''may''' effect your RSS feed, because it places an img tag in the excerpt, instead of text. So if you are going to do this, you probably want to set your options so that the full posts are put in RSS feeds, rather than excerpts. 告诫:以这种方式处理摘录'''可能'''影响你的RSS feed,因为将img标签放入摘录而不是文本中。因此,如果你准备执行这个操作,你可能想要设置你的选项,这样完整的文章而不是摘录就会放入RSS feeds。 === What categories do you show to the visitors? === ===你想要向访客显示什么类别? === You can limit the categories in archive and other pages with this code: 你可以使用这个代码在归档和其它页面中限制类别: <pre> <?php query_posts('cat=1&showposts='.get_option('posts_per_page')); ?> </pre> <pre> <?php query_posts('cat=1&showposts='.get_option('posts_per_page')); ?> </pre> This is placed before the Loop. 这个代码放置在Loop之前。 You find more info about the query_posts() function parameters here: http://codex.wordpress.org/User:JamesVL/query_posts . 你可以在这个网站找到更多的有关query_posts()函数参数的信息:http://codex.wordpress.org/User:JamesVL/query_posts 。 The <tt>get_option('posts_per_page')</tt> part of the code uses your blog's options to show a limited number of posts. 代码的<tt>get_option('posts_per_page')</tt>部分使用你的博客选项显示有限数目的文章。 This code is very useful if you would like to separate the categories. With this code you can make multiple archives. For example: 如果你想要分开类别,这个代码非常有用。使用这个代码,你可以创建多个归档。例如: <pre> <?php if (have_posts()) : ?> <?php if (is_month()) {query_posts('year='.get_the_time('Y').'&monthnum='.get_the_time('m').'&author_name=admin&cat=1&showposts='.get_option('posts_per_page')); ?> <!-- Do stuff... -> <?php } ?> <?php while (have_posts()) : the_post(); ?> <!-- post's contents --> <?php endwhile; ?> <?php endif; ?> </pre> <pre> <?php if (have_posts()) : ?> <?php if (is_month()) {query_posts('year='.get_the_time('Y').'&monthnum='.get_the_time('m').'&author_name=admin&cat=1&showposts='.get_option('posts_per_page')); ?> <!-- Do stuff... -> <?php } ?> <?php while (have_posts()) : the_post(); ?> <!-- post's contents --> <?php endwhile; ?> <?php endif; ?> </pre> If you use this code in <tt>archive.php</tt> and navigate to <tt>http://yourblog.com/2008/02/</tt>, show you the <tt>admin</tt> user's post that is posted on 2008.02. in the "first" category. 如果你在<tt>archive.php</tt>中使用这个代码并且导航到<tt>http://yourblog.com/2008/02/</tt>,向你显示<tt>admin</tt>用户的文章,在2008.02发表,在"第一个"类别中。 = Related = {{Tag Category Tags}} {{Query String Tag Footer}} = 相关的 = {{标签类别标签}} {{查询字符串标签页底文字}} = Further Reading = = 深入阅读 = * [http://wordpress.org/support/topic/38046 Support Forum discussion of Category-based theme] - how to make a Theme that organizes posts by category instead of by date on the home page * [[WordPress:Templates]] - Comprehensive list of resources related to Themes and Templates * [http://wordpress.org/support/topic/38046 支持论坛上以类别为基础的主题的讨论] – 怎样使得主题在主页上按照类别而不是安装日期来组织文章 * [[WordPress:Templates|模板]] – 与主题和模板相关的资源的全面列表
摘要:
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅
Wordpress-mediawiki:版权
的细节)。
未经许可,请勿提交受版权保护的作品!
取消
编辑帮助
(在新窗口中打开)
本页使用的模板:
模板:Query String Tag Footer
(
编辑
)
模板:Tag Category Tags
(
编辑
)
模板:查询字符串标签页底文字
(
编辑
)
模板:标签类别标签
(
编辑
)