Site icon @Poremsky.com

Create a list of pages in a specific category

I moved slipstick.com into WordPress as a CMS and wanted a list of articles in each category, on separate "index pages". On my old site, I used a fairly simple asp script to read one folder and list the files in the that directory. Each folder had its own directory.asp page.

The guys I hired didn't seem to understand how to do this in WordPress… (Tell me again why I hired someone to do this for me when I seem to be doing most of it on my own. ***)

I also wanted the home page to list posts of my choosing, and the guys finally came up with a page that displays posts in a specific category (used only for this purpose). The code they put in a page template (named "page-nn.php, where nn is the page id number) is below. (I recently switched to using a custom field coded by date.)

<?php query_posts( $query_string . '&cat=887' ); 
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<div id="post-<?php the_ID(); ?>" class="<?php hybrid_entry_class(); ?>">
<div class="entry-content">
<?php the_excerpt(); ?>

<?php wp_link_pages( array( 'before' => '<p class="page-links pages">' . __( 'Pages:', hybrid_get_textdomain() ), 'after' => '</p>' ) ); ?>

<?php comments_template( '/comments.php', true ); // Loads the comments.php template ?>
<?php endwhile; ?>

I did a little Binging (the quality of google searches are going downhill fast) and discovered a very simple way to do what I wanted, along with some code samples, but I also wanted to list all posts within the category on one page, not 10 per page (my posts per page/feed setting in WordPress). I found samples for that too.

To set the posts per page so that it displays all within the category without entering a specific number, you need to use -1 as the post count value: posts_per_page=-1.

<?php get_template_part( 'loop-meta' ); // Loads the loop-meta.php template. ?>

<?php query_posts('category_name=category_name&posts_per_page=-1&orderby=title&order=asc'); ?>

 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <li><a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a>  </li>

<?php endwhile; ?></textarea>

Each category gets its own page and rather than use page numbers like they did for the home page, I'm naming the pages page-slug.php – I need about 15 of these pages and that will be less confusing. I hope.

I'd wanted to insert this into a page so I can add notes or other information, rather than just having a list of pages. I can use widgets (the theme is built off of Hybrid) to add some text but would rather just type something directly in the page. Solution: loop twice.

<div id="content" class="hfeed content">

<div>
<!--use normal HTML for text that will be on all pages using this template. You won't see it when you create the page, but it will be shown when you save the page. -->
<p></p>
</div>

<!--Load the articles by tag -->
<?php query_posts('tag=' . the_slug() .'&orderby=title&order=asc'); ?>
  <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
     <div id="post-<?php the_ID(); ?>" class="hentry">
<!-- typical code snipped-->
           </div><!-- .hentry -->

<!-- reset the query-->
  <?php endwhile; wp_reset_query(); ?>
  <?php endif; ?>

<!--Next load the text typed into the editor.-->

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="<?php hybrid_entry_class(); ?>">
  <div class="hentry page"> 
      <?php the_content(); ?>
  </div><!-- .entry-content -->
  </div><!-- .hentry -->

***I hired a "WordPress developer" who came highly recommended from someone who I now think doesn't like me. 🙂 I knew enough about WordPress to get by (that's why I bought a theme for this site and one other one I own). The thought of moving a site with more than 1000 pages from flat HTML to WordPress was a daunting task, without trying to design it too, especially when I needed to convert an asp application to PHP to feed the Tools listing on many pages. I know even less about PHP… I also did not have unlimited free time and figured paying someone and getting it done was better than dragging this out for months.

They designed the site to look almost identical to my old site and set up some widgets that support widget logic but I was left on my own to learn widget logic… just as I had to figure how to create a list of pages by category (and more) out on my own.

I'm not real crazy about the same old, same ole… but now that its in WordPress, changing the theme will be fairly easy down the road.

Exit mobile version