Quantcast
Viewing latest article 8
Browse Latest Browse All 10

Using WordPress for Ecommerce, Part 54: Archive Page

Whether displaying a list of articles or a product grid, category pages are an important part of nearly any web development project.

I have taken a pause, if you will, in the Alien Authority development, project to look back and make sure that I have the site functioning just the way I want it before move into the next major section of development.

This article in the 54th installment of a series created to demonstrate how to use WordPress to develop an ecommerce site that sells virtual products from start to finish. The series addresses the (a) site-planning process, (b) graphic design, (c) plugin selection, (d) payment processing setup, and (e) virtual product delivery. This series also provides specific site-coding examples.

The Series to Date

In the previous article, I mentioned that I don’t believe there is any such thing as a perfect development project, and frankly when I am building a site as an illustration for an article series that project can become even more disjointed.

As such, I took a step back from my development schedule to really look through the Alien Authority website, which as you may know from earlier articles in this series will sell alien abduction certificates.

In the last installment, I made changes to the search form and the search results page.

In a similar fashion, in this article I am going to update the Alien Authority’s article and video category pages, or rather the template for these pages. At the moment this page does not display properly.

Image may be NSFW.
Clik here to view.
The Alien Authority site's category pages were not displaying properly.

The Alien Authority site’s category pages were not displaying properly.

Create an Archive Page Template

To reformat this page — and give it the appropriate content — I created a new PHP document and gave it the standard WordPress template comment.

  1. <?php
  2. /**
  3.  * The template for displaying Archive pages.
  4.  *
  5.  *
  6.  * @package WordPress
  7.  * @subpackage aa
  8.  * @since aa 1.0
  9.  */
  10.  
  11. get_header(); ?>
<?php
/**
 * The template for displaying Archive pages.
 *
 *
 * @package WordPress
 * @subpackage aa
 * @since aa 1.0
 */

get_header(); ?>

This file needs to be called archive.php or category.php, so that WordPress knows when to use it.

WordPress uses a template hierarchy to decide which template to use for a particular request. The archive.php file is the most generic archive-type template. For the Alien Authority site, it will be fine to manage category pages housing lists of articles, but I could have also chosen to be a bit more specific and named this file category.php. It is also possible to have archive type templates for authors, tags, taxonomies, dates, or even custom post types.

Next, the archive.php file needs a WordPress loop. The loop has been a common feature on every template created for the Alien Authority site.

  1. <div id="content" role="main">
  2.  
  3.   <h1 class="page-title"><?php single_cat_title(); ?> </h1>
  4.   <?php query_posts( 'posts_per_page=10' ); ?>
  5.   <?php if ( have_posts() ) : ?>
  6.   <?php /* Start the Loop */ ?>
  7.     <?php while ( have_posts() ) : the_post(); ?>
  8.       <ul id="category-list">
  9.         <li>
  10.           <h3><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h3>  
  11.           <?php the_excerpt(); ?>        
  12.         </li>
  13.       </ul>
  14.     <?php endwhile; ?>
  15.   <div class="pagination">
  16.     <?php previous_posts_link(__('Newer Entries')) ?>
  17.     <?php next_posts_link(__('Older Entries')) ?>
  18.   </div> <!-- end .pagination -->
  19.  
  20.   <?php else : ?>
  21.  
  22.   <article id="post-0" class="post no-results not-found">
  23.     <h1 class="entry-title">Nothing Found</h1>
  24.   </article><!-- #post-0 -->
  25.  
  26.   <?php endif; wp_reset_query(); ?>
  27.  
  28. </div><!-- #content -->
<div id="content" role="main">

	<h1 class="page-title"><?php single_cat_title(); ?> </h1>
	<?php query_posts( 'posts_per_page=10' ); ?>
	<?php if ( have_posts() ) : ?>
	<?php /* Start the Loop */ ?>
		<?php while ( have_posts() ) : the_post(); ?>
			<ul id="category-list">
				<li>
					<h3><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h3>		
					<?php the_excerpt(); ?>					
				</li>
			</ul>
		<?php endwhile; ?>
	<div class="pagination">
		<?php previous_posts_link(__('Newer Entries')) ?>
		<?php next_posts_link(__('Older Entries')) ?>
	</div> <!-- end .pagination -->

	<?php else : ?>

	<article id="post-0" class="post no-results not-found">
		<h1 class="entry-title">Nothing Found</h1>
	</article><!-- #post-0 -->

	<?php endif; wp_reset_query(); ?>

</div><!-- #content -->

For this particular loop, notice that I am setting the first h1 element to a value of the category’s title using the WordPress function single_cat_title().

  1. <h1 class="page-title"><?php single_cat_title(); ?> </h1>
<h1 class="page-title"><?php single_cat_title(); ?> </h1>

As with other loops, the content is added to the page using a while statement. One difference here, however, is that I have limited the page so that it shows just the most recent ten posts.

  1. <?php query_posts( 'posts_per_page=10' ); ?>
<?php query_posts( 'posts_per_page=10' ); ?>

Lower in the code, I reset the query.

  1. <?php endif; wp_reset_query(); ?>
<?php endif; wp_reset_query(); ?>

I also added pagination, so that users can click to see the next ten posts.

  1. <div class="pagination">
  2.   <?php previous_posts_link(__('Newer Entries')) ?>
  3.   <?php next_posts_link(__('Older Entries')) ?>
  4. </div> <!-- end .pagination -->
<div class="pagination">
	<?php previous_posts_link(__('Newer Entries')) ?>
	<?php next_posts_link(__('Older Entries')) ?>
</div> <!-- end .pagination -->

I also added the WordPress footer.

  1. <?php get_footer(); ?>
<?php get_footer(); ?>

I added some styles, leveraging the code used in the previous article. This code was from style.css.

  1. /* SERP and Catalog */
  2. ul#serp-list, ul#category-list {
  3.   margin: 0;
  4.   padding: 0;
  5.   list-style: none;
  6. }
  7.  
  8. ul#serp-list li h3, ul#category-list li h3 { margin-bottom: 5px; }
  9. ul#serp-list li p, ul#category-list li p { margin-top: 0; }
  10.  
  11. ul#category-list li { margin-bottom: 20px; border-bottom: 1px dotted #282828; }
  12. ul#category-list li:last-child { border-bottom: none; }
/* SERP and Catalog */
ul#serp-list, ul#category-list { 
	margin: 0;
	padding: 0;
	list-style: none;
}

ul#serp-list li h3, ul#category-list li h3 { margin-bottom: 5px; }
ul#serp-list li p, ul#category-list li p { margin-top: 0; }

ul#category-list li { margin-bottom: 20px; border-bottom: 1px dotted #282828; }
ul#category-list li:last-child { border-bottom: none; }

With these changes, the category pages are now properly laid out.

Image may be NSFW.
Clik here to view.
The updated archive.php file lays out the category page.

The updated archive.php file lays out the category page.

Summing Up

In this article, I updated the archive.php file responsible for adding content to and laying out the category pages on the Alien Authority website.

Related Articles


Viewing latest article 8
Browse Latest Browse All 10

Trending Articles