I'm trying to get a piece of code I can use sitewide to print lists of posts (like people commonly do on their homepage to show their categories' most recent). With help from others over past years, I've gotten a certain distance, but not all the way, which is what I'm trying now.
You can see this piece goes in "functions.php" I can make it get categories OR tags, offset them. I would like to add to this though. I would like to be able to select a category OR tag, but then exclude posts that also have another tag/cat. For example, I want to print a list of "grizzly-bear" tag posts, but exclude any posts tagged "alaska" as well as (if possible) any post from the "blog" category. I'd also prefer if the function was changed so that I could enter the number of posts the same way I do the offset (because the way it is now, I have to make a new function for each number of posts I want).
Right now I print it with:
<?php my_custom_loop(NULL, 'blogs', 0); ?>
Can anyone assist with how to add that stuff? Particularly with changing this code so that the number of posts, kind of so it'd be printable with something like:
<?php my_custom_loop(NULL, 'blogs', 0, numberofposts, excludecategoryorNULLL, excludetagorNULL); ?>
Function as it is now:
// Universal category - tag - offset code function
function my_custom_loop($category, $tag, $offset) {
$args=array(
// showposts has been replaced, use 'posts_per_page' instead
// 'showposts' =>10,
'posts_per_page' => 10,
// this has been replaced, use 'ignore_sticky_posts'
// 'caller_get_posts' => 1,
'ignore_sticky_posts' => true,
);
if ($category) {
$args['cat'] = $category;
}
if ($tag) {
$args['tag'] = $tag;
}
if ($offset) {
$args['offset'] = $offset;
}
$my_query = new WP_Query($args);
// ... rest of function to output loop
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" >
<?php the_title(); ?>
</a>
</li>
<?php
endwhile;
} //if ($my_query)
wp_reset_query(); // Restore global post data stomped by the_post().
};