Forums

[resolved] How to call query_posts from functions.php? (3 posts)

  1. madsphi
    Member
    Posted 7 months ago #

    Hi,

    I need to create a function in functions.php that goes through all the posts in a specific category (to display their titles and some related pictures).

    I have created the first version of the function like this:

    function showPosts($category_name)
    {
    query_posts('category_name=$category_name&showposts=10');

    while (have_posts()) : the_post();
    $title = get_the_title();
    endwhile;
    }

    Problem is that nothing is displayed. It looks like the query_posts call fails, but why is that? And what can I do about it?

    Thanks,
    Mads

  2. NateJacobs
    Member
    Posted 7 months ago #

    You'll need to either return the array from query posts or echo the values. Here is an example to get you started.

    https://gist.github.com/1263835

  3. madsphi
    Member
    Posted 7 months ago #

    Awesome. That was exactly what I needed.

    $args = array(
      'numberposts' => 10,
      'category_name' => $category_name
    );
    
    $posts = get_posts($args);		
    
    foreach ($posts as $post)
    {
    }

    Thanks a lot.

Reply

You must log in to post.

About this Topic