WordPress:Alphabetizing Posts

来自站长百科
Xxf3325讨论 | 贡献2008年7月24日 (四) 17:48的版本 (新页面: __TOC__ 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 o...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航、​ 搜索

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.

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:

<?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(); ?>

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.

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:

<?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(); ?>

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.

Index Templates

Maybe you want to list all your posts alphabetically on the main page. Edit your theme's 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(); ?>


References