Candy, A Journal by a James

Inserting pages into category archives in wordpress

Wouldn’t it be handy if you could have a little story at the top of your archive pages (or other tem­plates), explain­ing exactly what’s what? And wouldn’t it be handy if that little story was fully wysi­wyg edit­able? I thought so.

Sites in WordPress (WP) have “posts” which go into cat­egor­ies and “pages”, which are inde­pend­ent. My cun­ning plan was to name pages exactly the same as the cat­egor­ies I wanted them to describe, and then insert them into the archive page. It has been quite a search to get all the right code, even though the end res­ult looks quite simple!

The first thing you do, is cre­ate a cat­egory (”One & One”) and cre­ate a page (with the title “One & One”). Secondly, you open up your archive.php tem­plate file.

You should see some­thing like the fol­low­ing in it:

<?php if (have_posts()) : ?>

And also, fur­ther along:

<?php while (have_posts()) : the_post(); ?>


Those two state­ments start off “The Loop”, which is word­press lingo for the php-statements which pro­duce the desired out­put (like posts or lists of them) in a tem­plate. We’re going to put our blurb above it. You can also put it under “The Loop” (the “main” loop really) if you so wish — no extra code needed.

Simply put:

<?php

$get­cat­egory = $wp_query->get_queried_object();

$page­to­get = ‘page­name=’ . $getcategory->category_nicename;

$seperate_query = new WP_Query($pagetoget);

/* Next…we start our own WordPress Loop, based on the seper­ate query (as seen above). That way we can still use the main query! */?>

<?php while ($seperate_query->have_posts()) : $seperate_query->the_post(); ?>

<div id=“blurb”>

<h3>Standard blurb for: <?php echo single_cat_title(”, false); ?></h3>

<?php the_content() ?>

</div>

<br style=“clear:both;”>

<?php end­while; ?>

<?php rewind_posts(); /* This “rewinds” the loop, so we can use another loop after it safely.*/ ?>

Infront of the start of the main loop in your archive.php, so above of the fol­low­ing line:

<?php if (have_posts()) : ?>

And that’s all the code that’s needed!

Let me explain what the code actu­ally does. The first two lines — $get­cat­egory = $wp_query->get_queried_object(); $page­to­get = ‘page­name=’ . $getcategory->category_nicename;  — ask WP for the “cat­egory slug” or the “nice­name” of the cat­egory. That’s the san­it­ized ver­sion of “One & One” in this case, which is “one-one”.

This “cat­egory slug” is handy, because “one-one” will also be the “page slug”, so it’ll match the san­it­ized name of the page we want to insert into our cat­egory archive. We get our page by doing — $seperate_query = new WP_Query($pagetoget); while ($seperate_query->have_posts()) : $seperate_query->the_post(); — that starts a new, com­pletely seper­ate loop from the main loop that will follow.

Then all that’s left is to insert the page’s con­tent like so — <?php the_content() ?> — and then end and rewind the loop with the fol­low­ing state­ments — <?php end­while; ?> <?php rewind_posts(); ?> .

You can see I’ve added a div around the content-insertion and <br style=“clear:both;”> after it. That pre­pares you for any floating-images that may be part of the inser­ted page’s con­tent. The <h3>Standard blurb for: <?php echo single_cat_title(”, false); ?></h3> is there mainly to know what I’m doing (when work­ing on it) and to seper­ate it out from the list­ing of cat­egory con­tent that will fol­low after the blurb. I’ve left it in the example so you know how to get both the san­it­ized “cat­egory slug” and the reg­u­lar “cat­egory title”, which this produces.

This code works in the stand­ard WordPress 2.6 tem­plate based on K2 and should work in pretty much all other tem­plates too. Many thanks to Joen and Google for their help.

Comments are closed.