WordPress:Alphabetizing Posts

来自站长百科
跳转至: 导航、​ 搜索



By default, WordPress organizes and displays posts in descending chronological order: newest first, oldest last. Sometimes, though, you might want to list posts in alphabetical order. Using different templates in WordPress, you may implement post listings in an alternative manner.

默认情况下,WordPress是以相反的时间顺序组织并且显示文章的是:最新发表的文章最新显示,最早写的文章最后显示。但是,有时候,你可能想要按字母表顺序显示文章。使用WordPress中不同的模板,你可能使用另一种方式显示文章列表。

Category Template[ ]

类别标签[ ]

For example, say you have a category named "Glossary" that serves as (obviously) a glossary, where each post is a definition of a specific term, and each term is used as the post title. You want one master list of all the terms, plus their definitions. Edit your theme's category.php file, and make the following changes, just before WordPress:The Loop:

例如,假如你有一个类别,名称为"术语表"作为(明显地)术语表,每篇文章定义了某个术语,那个术语就作为文章的标题。你想要所有术语,以及这些术语的定义的一个列表。编辑你的主题category.php文件,并且更改,在The Loop之前:

<?php
get_header();
?>

<div id="content">

<?php
// we add this, to show all posts in our
// Glossary sorted alphabetically
if (is_category('Glossary')) 
{
     $posts = query_posts($query_string . 
'&orderby=title&order=asc&posts_per_page=-1');
} 
// here comes The Loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>



<?php
get_header();
?>

<div id="content">

<?php
// 我们添加这个,
// 按字母表顺序,在术语表中显示所有的文章
if (is_category('Glossary')) 
{
     $posts = query_posts($query_string . 
'&orderby=title&order=asc&posts_per_page=-1');
} 
// 下面是 Loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>



Specific Category Template[ ]

特别类别模板[ ]

If you want your "Glossary" category to have a very different style from the rest of your site, you could create a custom category template file just for it. First, find the category ID of your "Glossary" category. The category ID is listed in the left-most column of your Manage > Categories administration panel. For this example, we'll assume that the "Glossary" category has a category ID of 13.

如果你希望"术语表"类别的样式与站点的其它部分的样式不同,你可以为术语表创建一个自定义类别模板文件。首先,找到你的"术语表"类别的ID。类别ID列在管理 > 类别管理面板最左边的栏中。在这个例子中,我们假定"术语表"类别的ID是13.

Copy your theme's index.php or category.php file (or create a brand new file, if necessary) named category-13.php, and insert your code as needed:

复制你的主题的index.php 或者 category.php 文件(或者如果必须的话,创建一个新文件)命名为category-13.php,如果需要的话,插入你的代码:


<?php
get_header();
?>

<div id="content">

<?php
// we add this, to show all posts in our 
// Glossary sorted alphabetically
$posts = query_posts($query_string . 
'&orderby=title&order=asc&posts_per_page=-1');
// here comes The Loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>


<?php
get_header();
?>

<div id="content">

<?php
// 我们添加这个,
 //以字母表顺序,在术语表中显示所有文章
$posts = query_posts($query_string . 
'&orderby=title&order=asc&posts_per_page=-1');
// 下面是Loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>




Using a separate category template file for your "Glossary" category means that you do not clutter up your main category.php file with a bunch of conditional template tags.

为你的"术语表"类别使用单独的类别模板文件,意味着你不需要将主要的category.php文件和一些条件式标签混乱在一起。

Index Templates[ ]

索引模板[ ]

Maybe you want to list all your posts alphabetically on the main page. Edit your theme's index.php:

也许你想要在主页上按字母表顺序列表你的所有的文章。编辑你的主题的index.php

<?php
// we add this, to show *all* posts sorted
// alphabetically by title
     $posts = query_posts($query_string . '&orderby=title&order=asc&posts_per_page=-1');
// here comes The Loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>


<?php
wp_get_archives('type=postbypost&sort=post_title&order=ASC'); 
?>

-->

References[ ]

参考[ ]