Creating an HTML sitemap *without* a plugin?
-
I want to add an HTML sitemap template to my site. (I have an XML sitemap for search engines, but it’s not really designed for human users.) However, I would really rather not add yet another plugin, so I’d instead like to create one as a page template. Unfortunately, I know very little about PHP functions, so I’m having trouble getting it to behave the way I’d like.
What I need the sitemap to do is display all posts by category, sorted by post title rather than by date or ID number.
The parent template I’m currently using (Frontier) has a page-sitemap template, but it’s set up to display only a list of the 30 most recent posts in descending date order, not organized by category:
<ul><?php $archive_query = new WP_Query('showposts=30&cat=-8'); while ($archive_query->have_posts()) : $archive_query->the_post(); ?> <li> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a> </li> <?php endwhile; ?> </ul>
I tried using the code suggested by Joost de Valk (here):
<ul> <?php // Add categories you'd like to exclude in the exclude here $cats = get_categories('exclude='); foreach ($cats as $cat) { echo "<li><h3>".$cat->cat_name."</h3>"; echo "<ul>"; query_posts('posts_per_page=-1&cat='.$cat->cat_ID); while(have_posts()) { the_post(); $category = get_the_category(); echo "</ul>"; echo "</li>"; } ?> </ul>
(I had to remove Joost’s original if statement to be able to choose under which category posts with multiple categories appear).
This displays all posts grouped by category, which is closer to what I want, but there are two problems:
1) It uses the query_posts() function, which the WordPress Codex emphatically advises not using for template files. The Codex’s caveats gave me pause and made me think this is not the best way to do it.
2) It still sorts posts by descending date order, not by title.
I’ve been staring at the codex pages for get_posts and get_category to see if I can figure out a better way to do it, but a lot of it is way beyond my skill level.
What is the best way to do this? Again, I really, really don’t want to add another plugin, especially when I’ve almost got something workable.
Thanks!
- The topic ‘Creating an HTML sitemap *without* a plugin?’ is closed to new replies.