The days nemesis has been vanquished!
- 0
- Add a Comment
This might seem a bit lame to those who actually know what they’re doing, but I have been bashing my head against the wall trying to figure out how to grab and display more than one post in a category using the archive.php file.
I’m using the archive.php file instead of creating a specific category file because I want to keep the layout the same with the least amount of editing whenever I do need to make a change. Copy pasta maybe easy, but it soon gets tiresome and is needless extra hassle.
After some frantic “help!” posts on twitter (thank you @wpbeginner for responding!) and friendfeed plus a little searching of my own, all roads seemed to lead to this page in the wordpress codex.
Specifically, this solution:
<?php
$categoryvariable=$cat; // assign the variable as current category
$query= ‘cat=’ . $categoryvariable. ‘&orderby=date&order=ASC’; //concatenate the query
query_posts($query); //run the query
?>
Now, dumping this just before the while (have_posts()) : the_post(); line, ensuring you swap out ($query) with (’showposts=10&cat=’.$cat) will give you what you want - upto 10 posts of the category selected by the user.
Sadly, this will break the displaying of your tags, which simply won’t do! Off to the drawing board again!
My starter for ten was to try creating my own bunch of if statements to alter the query based on whether it was a category or a tag just before my while statement. No good, broke the whole page instead of just a specific functionality. Then a lightbulb flickered inside.
There’s already an if statement determining what to do based on the users actions right at the top… why don’t I just sneak them into there? I’m cutting down on additional if statements and making the code cleaner. So, I bodged together the code and got the following:
<?php if (have_posts()) : ?>
<div class=”item entry”>
<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<h2 class=”pagetitle”><?php single_cat_title(); ?></h2>
<?php
$categoryvariable=$cat;
$query= ‘cat=’ . $categoryvariable. ‘&orderby=date&order=ASC’;
?>
<?php query_posts(’showposts=10&cat=’.$cat); ?>
<?php /* If this is a tag archive */ } elseif( function_exists(’is_tag’) ) { if(is_tag()){ ?>
<h2 class=”pagetitle”>Episodes mentioning <?php single_tag_title(); ?></h2>
<?php
$tagvariable=$tag;
$query= ‘tag=’ . $tagvariable. ‘&orderby=date&order=ASC’;
?>
<?php query_posts(’showposts=10&tag=’.$tag); ?>
Hit refresh and began testing…. it works! No breaking, no spooling out the wrong info!
Check it yourself at http://dev.verticalslice.tv by clicking on “News” on the left and playing around.
Not bad for someone who only knows basic HTML and lacks any knowledge of how wordpress functions internally. Tho, as I said, it’s still prolly lame to those who actually know what they’re doing - but I’m not them, and this was my victory
If you know of a better way of doing this or if this opens the site up to a vulnerability, please let me know in the comments.