Wordpress - trying to create a function with the loop but not see the posts

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');

Answers


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');

Need Your Help

Append a number to every php instance

php

I have a form in my website where users can send messages to multiple recipients, they type in the recipient's numbers separated by commas, I recieve the numbers in my php script as a single post

How to programmatically check if an application has deployed in JBoss5?

java jboss java-ee jboss5.x ear

I am writing a small app to automate deployments. Basically, it checks an ftp server periodically for a new .ear file. Compares the checksum of the remote ftp file against what is currently deplo...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.