query_posts in a reusable function?
Hi all 🙂
I had a reusable function that was working terrifically – it was a simple function where you’d input a category name and a number, and the function would take the category name, convert it to an ID, and pop that into a query_posts section (the number was to show how many posts). For example:
$catid = get_cat_id($catname); query_posts(array('cat' => '$catid', 'showposts' => '$num', 'post_status' => 'publish'));
However, I see that in WP 3.0.x, “showposts” has been deprectaed, and been replaced by “posts_per_page”.
My issue is that the function no longer works. It now simply pulls the most recent post of the chosen category, no matter what. I can get this code to work if I manually put it in the template file of my choosing, but that means that *anywhere* I want to use it, I have to rewrite this code, rather than use the nice, neat, reusable function.
Has something happened so you can no longer use query_posts (and other versions thereof – I’ve tried using new WP_Query, parse_query($args), etc. all to no avail.)
If it helps, here’s the full code for the function. For the record, newsletter_date() is another function written, all it does is echo out a month and year – nothing more. I’m using it for comparison.
So to call the function into the template file, you’d do something like this:
<?php echo newsletter_cats('Bunko', '5'); ?>
The idea would then be that the most recent 5 posts in the “Bunko” category would show up on the page.
Can anyone see why this can no longer work to show how many posts I need to output? All it’s doing is returning the most recent post in the category. I’ve been trying for days to figure this out, and I’m at my wit’s end.
Thanks in advance for any help you can give.
- The topic ‘query_posts in a reusable function?’ is closed to new replies.