Reputation: 4711
So I'm trying to create a simple list of posts from a certain category and I wanted to put that function in my functions.php so I could just call the short code.
I get the list I want however, I also get the full posts below the list... ? I dont want the posts.
(Also wondering how to get these posts in reverse Chronological order)...
Here is my function:
function getVolArchives() {
query_posts('category_name=volunteerspotlights&showposts=12');
echo '<ul style="list-style: none;">';
while (have_posts()) : the_post();
echo '<li style="width: 50%; float: left;"><a href="'. get_permalink() . '"><div style="font-size: 14px; font-weight: bold;">' . get_the_title() . '</div><div class="archiveName">' . get_the_date('F') . '</div></a></li>';
endwhile;
echo '</ul>';
}
add_shortcode('getVolArchives', 'getVolArchives');
Upvotes: 0
Views: 65
Reputation: 834
Regarding the sorting you can find all the acceptable parameters for query_posts() here in the class reference for WP_Query which query_posts uses - order and orderby specifically, will handle your sorting needs.
Also, I'm not sure if this will address your issue with the full posts, but in my experience it was best to build a string with these functions and return the string rather than echo the content out directly.
I'm a little rusty regarding WP at the moment, so someone else may be better able to conjure up a better fix/example:
function getVolArchives() {
query_posts('category_name=volunteerspotlights&showposts=12&orderby=date&order=DESC');
$return_string = '<ul style="list-style: none;">';
while (have_posts()) : the_post();
$return_string .= '<li style="width: 50%; float: left;"><a href="'. get_permalink() . '"><div style="font-size: 14px; font-weight: bold;">' . get_the_title() . '</div><div class="archiveName">' . get_the_date('F') . '</div></a></li>';
endwhile;
$return_string = '</ul>';
return $return_string;
}
add_shortcode('getVolArchives', 'getVolArchives');
Upvotes: 1