• Resolved Ate Up With Motor

    (@ate-up-with-motor)


    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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • 1. You could easily replace query_posts() with WP_Query(). In terms of parameters, the two functions are freely inter-changeable.

    2. Check out the orderby parameter in https://codex.www.ads-software.com/Function_Reference/WP_Query

    Thread Starter Ate Up With Motor

    (@ate-up-with-motor)

    Ahah, I got that to work instead of query_post() — now I have to see if I can get the orderby syntax right.

    Thanks!

    Thread Starter Ate Up With Motor

    (@ate-up-with-motor)

    Okay, I think I’ve got it. For the benefit of others, here is what I finally ended up with:

    <?php
    // Add categories to exclude in the exclude here
    $cats = get_categories('exclude=');
    foreach ($cats as $cat) {
      echo "<h3>".$cat->cat_name."</h3>";
      echo "<ul>";
      $archive_query = new WP_Query('posts_per_page=-1&order=asc&orderby=title&cat='.$cat->cat_ID);
      while ($archive_query->have_posts()) { $archive_query->the_post();
        $category = get_the_category();
    	  echo '<li><a href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></li>';
    	  }
      echo "</ul>";
    }
    ?>

    That produces a list of all posts grouped by category and then sorted in ascending alphabetic order by post title.

    Thanks, esmi! (And thanks again to Joost de Valk for getting me most of the way there.)

    Glad I could help ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Creating an HTML sitemap *without* a plugin?’ is closed to new replies.