个人工具
名字空间
变换
操作

WordPress:Linking Posts Pages and Categories

来自站长百科
2008年4月19日 (六) 02:53Fludlen (讨论 | 贡献)的版本
跳转到: 导航, 搜索

There are two ways to link to content pages in WordPress. One uses permalinks and one does not. The method that does not use permalinks works regardless of whether permalinks are enabled for your site.

有两种方法可以链接到WordPress的内容网页。一种使用permalinks,另一种没有使用。另一种不使用permalinks的方法,不管你的网站上有没有嵌入permalinks,都可以使用这种方法。

目录

Linking Without Using Permalinks

没有使用Permalinks的链接

If you are not using Permalinks, how do you link to your pages, posts, and categories?

如果你没有使用Permalinks, 你怎样链接到你的网页,文章,和类别中呢?

Using the numeric values found in the ID column of the Posts, Categories, and Pages Administration, you can create links as follows.

使用ID专栏的文章,类别,和网页管理的数值,你可以创建以下的链接。

Posts

文章

To link to a Post, find the ID of the target post on the Posts administration panel, and insert it in place of the '123' in this link:

要链接到一篇文章,在文章管理面板上找到所要链接文章的ID,并将它插入到这个链接的'123'的位置:

<a href="index.php?p=123">Post Title</a>

<a href="index.php?p=123">Post Title</a>


Categories

类别

To link to a Category, find the ID of the target Category on the Categories administration panel, and insert it in place of the '7' in this link:

链接到一个类别,找到类别管理面板上所要找的类别的ID,并将它插入到这个链接'7'的位置,取代'7':

<a href="index.php?cat=7">Category Title</a>

<a href="index.php?cat=7">Category Title</a>

Pages

网页

To link to a Page, find the ID of the target Page on the Pages administration panel, and insert it in place of the '42' in this link:

链接到一个网页,找到管理面板上所要找的网页的ID,经它插入到这个链接的'42',取代'42':

<a href="index.php?page_id=42">Page title</a>

<a href="index.php?page_id=42">Page title</a>


Date-based Archives

以日期为基础的归档

  • Year: <a href="index.php?m=2006">2006</a>
  • Month: <a href="index.php?m=200601">Jan 2006</a>
  • Day: <a href="index.php?m=20060101">Jan 1, 2006</a>
  • 年: <a href="index.php?m=2006">2006</a>
  • 月: <a href="index.php?m=200601">Jan 2006</a>
  • 日: <a href="index.php?m=20060101">Jan 1, 2006</a>


Links On External Sites

外部站点的链接

If you are providing a link to your site from outside of your site, be sure to specify a full URL to the correct location:

如果你给你的站点提供一个外部链接,确保指定一个完整的URL到一个正确的地址:

<a href="http://example.com/index.php?p=123">post title</a>
<a href="http://example.com/index.php?p=123">post title</a>


If you have installed WordPress to a subfolder, such as wordpress, don't forget to add the folder to the link URL: 如果你在一个子文件夹中安装了WordPress, 例如wordpress, 别忘了将文件夹添加到链接URL上:

<a href="http://example.com/wordpress/index.php?p=123">post title</a>


<a href="http://example.com/wordpress/index.php?p=123">post title</a>


Linking Using Permalinks

链接使用 Permalinks

If you are using permalinks, you can use all of the above non-permalink techniques, which will work with permalinks enabled or not. If you have enabled permalinks, you have a few additional options for providing links that readers of your site will find a bit more user-friendly than the cryptic numbers.

如果你正使用permalinks, 你可以使用以上所有的非permalinks方法,这些方法会对激活的或者没有激活的permalinks起作用。如果你激活了permalinks,通过链接时,你就拥有了一些额外的选择,你站点的用户会觉得,比含义模糊的数字,你那样做显得更加友好。

The complexity of the URL depends on the complexity of your permalink configuration. If your permalink configuration (set on the Options > Permalinks Administration Panel) contains many Structure Tags, then the URL will be more difficult to construct.

URL的复杂性取决于你的permalinks配置的复杂性。如果你的permalinks配置(在选项 > Permalinks设置的)包含了许多的结构标签, 那么要创建URL就会变得更难。

Posts

文章

For posts, replace each Structure Tag in your permalink structure with the data appropriate to a post to construct a URL for that post. For example, if the permalink structure is:

对于文章,将你的permalink结构中每个结构标签替换为合乎文章的数据,来为那篇文章建立一个URL。例如,如果permalink结构是:

/index.php/archives/%year%/%monthnum%/%day%/%postname%/

/index.php/archives/%year%/%monthnum%/%day%/%postname%/


Replacing the Structure Tags with appropriate values may produce a URL that looks like this:

将结构标签替换为合适的值,可能会产生一个URL, 看起来像:

<a href="/index.php/archives/2005/04/22/my-sample-post/">My Sample Post</a>

<a href="/index.php/archives/2005/04/22/my-sample-post/">我的样本文章</a>


To obtain an accurate URL for a post it may be easier to navigate to the post within the WordPress blog and then copy the URL from one of the blog links that WordPress generates.

要为文章获得一个精确的URL, 导航到WordPress博客内部的文章,然后复制WordPress产生的其中的一个博客链接,会更加地简单。

Review the information at WordPress:Using Permalinks for more details on constructing URLs for individual posts.

回顾使用Permalinks中的信息,得到更多的关于为单个文章建立URLs的信息。

Categories

类别

To produce a link to a Category using permalinks, obtain the Category Base value from the Options > Permalinks Administration Panel, and append the category name to the end.

为使用permalinks的类别产生一个链接,从选项 > Permalinks管理面板中获取类别基本值,在最后附加上类别名。

For example, to link to the category "testing" when the Category Base is "/index.php/categories", use the following link:

例如,当类别基数是"/index.php/categories"的时候,链接到类别"测试",使用以下的链接:

<a href="/index.php/categories/testing/">category link</a>

<a href="/index.php/categories/testing/">category link</a>


You can specify a link to a subcategory by using the subcategory directly (as above), or by specifying all parent categories before the category in the URL, like this:

通过直接地使用子目录(如上),或者通过经所有的母类别指定在URL中的类别前面,像以下的,你就可以指定一个链接到子类别中:

<a href="/index.php/categories/parent_category/sub_category/">subcategory link</a>


<a href="/index.php/categories/parent_category/sub_category/">subcategory link</a>


Pages

网页

Pages have a hierarchy like Categories, and can have parents. If a Page is at the root level of the hierarchy, you can specify just the Page's "page slug" after the static part of your permalink structure:

网页就像类别一样,可以有层级,可以有父母(上一级网页)。如果一个网页在一个在一个层级的根级别上,你只要指定网页的"page slug"到你的permalink结构的后面:

<a href="/index.php/a-test-page">a test page</a>

<a href="/index.php/a-test-page">a test page</a>


Once again, the best way to verify that this is the correct URL is to navigate to the target Page on the blog and compare the URL to the one you want to use in the link.

再一次地,查证这是一个正确的URL的最好的方法是,导航到博客中的目标网页,并且将URL与你在链接中使用的URL进行比较。

Date-based Archives

以日期为基础的归档

  • Year: <a href="/index.php/archives/2006">2006</a>
  • Month: <a href="/index.php/archives/2006/01/">Jan 2006</a>
  • Day: <a href="/index.php/archives/2006/01/01/">Jan 1, 2006</a>


  • 年: <a href="/index.php/archives/2006">2006</a>
  • 月: <a href="/index.php/archives/2006/01/">Jan 2006</a>
  • 日: <a href="/index.php/archives/2006/01/01/">Jan 1, 2006</a>


Links on External Sites

外部站点的链接

Permalink structures should begin with a slash, meaning that they are anchored at the root of the site's URL. You should be able to prepent the protocol and server name to any link that begins with a slash to build a successful full URL.

Permalink 结构应该以一个斜线为基础,意思是它们被锚定在站点的URL的根上。你应该能够perpent协议和服务器名到任何以一个斜线为开始的链接的,来创建一个成功的完整的URL。

For example, this category link: 例如,这个类别链接:

<a href="/index.php/categories/parent_category/sub_category/">subcategory link</a>

<a href="/index.php/categories/parent_category/sub_category/">subcategory link</a>


Becomes this category link using a full URL: 变成了这个类别链接,使用一个完整的URL:

<a href="http://example.com/index.php/categories/parent_category/sub_category/">subcategory link</a>
<a href="http://example.com/index.php/categories/parent_category/sub_category/">subcategory link</a>


Combining Links with Template Tags

将链接与模板标签联合

You can customize your links in the header, footer, or sidebar to be combinations of link types. This example features links to two categories, the main index page, a post, a static page, and uses the Pages template tag.

你可以在页眉,页脚或者工具条中将你的链接自定义为链接的结合体。这个例子的特点是链接到两个类别,主要的索引页,一篇文章,一个静态的页面,并且使用了网页模板标签。

Note carefully that the wp_list_pages() template tag generates its own List Item (LI) so it doesn't need to be wrapped in a LI tag. This template tag is also set to list only the parent Pages and not their subPages or "children".

仔细注意,wp_list_pages()模板标签产生了它自己的列表条目(LI),这样它就不需要被包在一个LI标签中。这个模板标签也被设置为只列出母网页(上级网页),而不列出他们的子网页或者叫"孩子"。

<ul id="linklist">
 <li>
  <?php _e('Check It Out'); ?>
  <ul id="pageslist">
   <li>
    <a title="Home Page" href="index.php">Home</a>
   </li>
   <li>
    <a title="Blog" href="index.php?cat=7">Blog</a>
   </li>
   <li>
    <a title="Life Story" href="index.php?p=12">My Life Story</a>
   </li>
   <?php wp_list_pages('exclude=4&depth=1&sort_column=menu_order&title_li='); ?>
   <li>
    <a title="Links and Resources" href="index.php?cat=33">Links</a>
   </li>
   <li>
    <a title="Site Map" href="sitemap.php">Site Map</a>
   </li>
  </ul>
 </li>
</ul>




<ul id="linklist">
 <li>
  <?php _e('Check It Out'); ?>
  <ul id="pageslist">
   <li>
    <a title="Home Page" href="index.php">Home</a>
   </li>
   <li>
    <a title="Blog" href="index.php?cat=7">Blog</a>
   </li>
   <li>
    <a title="Life Story" href="index.php?p=12">My Life Story</a>
   </li>
   <?php wp_list_pages('exclude=4&depth=1&sort_column=menu_order&title_li='); ?>
   <li>
    <a title="Links and Resources" href="index.php?cat=33">Links</a>
   </li>
   <li>
    <a title="Site Map" href="sitemap.php">Site Map</a>
   </li>
  </ul>
 </li>
</ul>

Using such a customized list, you can also add CSS classes to change the look of each of the links, or style the entire section. It's up to you.

使用这个自定义的列表,你也可以添加CSS类别来改变每个链接的外观,或者设计这个部分。这都取决于你自己。

Absolute versus Relative Links

完全的与相对的链接

Absolute links define absolutely where to find the target of the link.
Relative links define the location of another document in relation to the current document.

Absolute链接定义了absolutely在哪儿可以找到目标链接。
Relative链接定义了与当前文件有关的另一个文件的位置。

Absolute Link Examples

完全链接的例子

Full URIs of the form http://example.com/wordpress/index.php are absolute links. 形式http://example.com/wordpress/index.php的完全URLs是完全链接。

Absolute links can also point to your own server. When doing so, you may safely omit the http://domain.com prefix, and link to the target with a full path:

/wordpress/index.php

The leading slash means "At the very top of this domain is a directory named wordpress, and inside this directory is a file named index.php".

完全的链接可以指向你自己的服务器。当你这样做时,你可能安全地省略了http://domain.com前缀,并且使用一个完全的路径链接到目标上:

/wordpress/index.php 第一个斜线的意思是"在这个域的最顶上方是一个目录,目录名是wordpress,在这个目录的里面,是一个文件,这个文件名是index.php".

A document at

http://example.com/wordpress/index.php

contains a link of the form

/wordpress/index.php

The link above, when clicked, will take the viewer to

http://example.com/wordpress/index.php

http://example.com/wordpress/index.php 中的一个文件 包含一个形式的链接 /wordpress/index.php 当你点击上面的这个链接的时候,这个链接会带你来到 http://example.com/wordpress/index.php


Relative Link Examples

相关的链接例子

Relative links do not start with a slash:

wordpress/index.php

The lack of a leading slash means "Inside the current directory is a sub-directory named wordpress, and inside that directory is a file named index.php".

相关的链接不是以一个斜线开始的: wordpress/index.php 缺少了一个引导的斜线,意思是"在目前的目录中是一个子目录,子目录名是wordpress, 在目录总是一个文件,文件名是index.php".

A document at

http://example.com/wordpress/index.php

contains a link of the form

wordpress/index.php

The link above, when clicked, will take the viewer to

http://example.com/wordpress/wordpress/index.php

http://example.com/wordpress/index.php 中的一个文件 包含一个这个形式的链接

wordpress/index.php

当你点击以上的链接的时候,这个链接会带你到 http://example.com/wordpress/wordpress/index.php



For more information on absolute and relative links, see the WebReference Tutorial on Absolute and Relative Links.

关于完全的和相关的链接的更多的信息,请看完全的和相对的链接的万维网参考指南

Dynamic Linking in Templates

模板中的动态链接

Whether you use permalinks or not, in templates you can link to pages or posts dynamically by referring to its unique numerical ID (seen in several pages in the admin interface) with <a href="<?php echo get_permalink(ID); ?>">This is a link</a>. (as shown in WordPress:Template_Tags/get_permalink)

不管你是否使用permalinks,你可以在模板中动态地链接到网页或者文章,只要通过用<a href="<?php echo get_permalink(ID); ?>">提及到文章或者网页的数字的ID(在管理界面的多个网页上可以看到)。这是一个链接</a>. (如[[WordPress:Template_Tags/得到_permalink]所显示的]) 。

This is a convenient way to create page menus as you can later change page slugs without breaking links, as IDs will stay the same. However, this might increase database queries.

这是创建网页菜单的一个方面的方法,因为你不久可以在不破坏链接的情况下,更改page slugs,因为ID都是保持相同的。然而,这可能会增加数据库查询。

Dynamic page menus can also be created by utilizing WordPress:Template_Tags/wp_list_pages child_of parameter or some of the many available plugins.

也可以使用WordPress:Template_Tags/wp_列表_网页参数的孩子(子参数)或者一些可以得到的插件来创建动态网页菜单。

Resources

资源