Wouldn’t it be handy if you could have a little story at the top of your archive pages (or other templates), explaining exactly what’s what? And wouldn’t it be handy if that little story was fully wysiwyg editable? I thought so.
Sites in WordPress (WP) have “posts” which go into categories and “pages”, which are independent. My cunning plan was to name pages exactly the same as the categories 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 result looks quite simple!
The first thing you do, is create a category (”One & One”) and create a page (with the title “One & One”). Secondly, you open up your archive.php template file.
You should see something like the following in it:
<?php if (have_posts()) : ?>
And also, further along:
<?php while (have_posts()) : the_post(); ?>
Those two statements start off “The Loop”, which is wordpress lingo for the php-statements which produce the desired output (like posts or lists of them) in a template. 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.
$getcategory = $wp_query->get_queried_object();
$pagetoget = ‘pagename=’ . $getcategory->category_nicename;
$seperate_query = new WP_Query($pagetoget);
/* Next…we start our own WordPress Loop, based on the seperate query (as seen above). That way we can still use the main query! */?>
<?php while ($seperate_query->have_posts()) : $seperate_query->the_post(); ?>
<h3>Standard blurb for: <?php echo single_cat_title(”, false); ?></h3>
<?php the_content() ?>
<?php endwhile; ?>
<?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 following line:
<?php if (have_posts()) : ?>
And that’s all the code that’s needed!
Let me explain what the code actually does. The first two lines — $getcategory = $wp_query->get_queried_object(); $pagetoget = ‘pagename=’ . $getcategory->category_nicename; — ask WP for the “category slug” or the “nicename” of the category. That’s the sanitized version of “One & One” in this case, which is “one-one”.
This “category slug” is handy, because “one-one” will also be the “page slug”, so it’ll match the sanitized name of the page we want to insert into our category 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, completely seperate loop from the main loop that will follow.
Then all that’s left is to insert the page’s content like so — <?php the_content() ?> — and then end and rewind the loop with the following statements — <?php endwhile; ?> <?php rewind_posts(); ?> .
You can see I’ve added a div around the content-insertion and <br style=”clear:both;”> after it. That prepares you for any floating-images that may be part of the inserted page’s content. The <h3>Standard blurb for: <?php echo single_cat_title(”, false); ?></h3> is there mainly to know what I’m doing (when working on it) and to seperate it out from the listing of category content that will follow after the blurb. I’ve left it in the example so you know how to get both the sanitized “category slug” and the regular “category title”, which this produces.
This code works in the standard WordPress 2.6 template based on K2 and should work in pretty much all other templates too. Many thanks to Joen and Google for their help.