• smijos

    (@smijos)


    It’s a simple thing, really – I want to use get_posts to retrieve posts from a category that is a child of another category. When I use any of the main category IDs, it works – for some reason, children categories don’t work.

    Am I crazy?

    <?php
    $posts = get_posts(‘numberposts=2&category=16’);
    foreach ($posts as $post) : start_wp(); ?>
    <– do stuff –>
    <?php endforeach; ?>

    here is the error:
    Warning: Invalid argument supplied for foreach()

    ALSO – the_title only works for the first post!!! DAAGH!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter smijos

    (@smijos)

    an hour later I worked out how to use query_posts instead.

    Thread Starter smijos

    (@smijos)

    the_title STILL only works for the first post, though

    Kafkaesqui

    (@kafkaesqui)

    What does your post loop look like? Is it something like:

    <?php
    query_posts('showposts=2&cat=16');
    while(have_posts()) : the_post(); ?>
    <-- do stuff -->
    <?php endwhile; ?>

    query_posts() will not work on a foreach().

    Thread Starter smijos

    (@smijos)

    Okay first off the error I got was because I had selected ONLY a child category for the post and not a parent as well.

    So get_posts, and query_post both seem to be working now with child categories.

    Thread Starter smijos

    (@smijos)

    Secondly, the_date still doesn’t work with the following loop (only shows the date for the first post)

    <?php query_posts(‘cat=16’); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <– do stuff –>
    <?php endwhile; ?>
    <?php endif; ?>

    I set it up that way because the codex says to use query_post outside the loop – is there another way?
    (the_title and the_excerpt work fine for all posts)

    Note the_date() only displays once for each day of posts (no matter the number of posts). Is this the problem you’re seeing? If so, try using the_time() with a date format string, or:

    <?php the_time(get_settings('date_format')); ?>

    Note that when we says query_posts() must be run before The Loop, we just mean before the if (have_posts()) : while (have_posts()) stuff occurs. The <php> tags don’t have an effect on this. But either method (separate or combined <php> tags) will work.

    Thread Starter smijos

    (@smijos)

    AH! I did not know that about the_date. Needless to say, your solution works perfectly!

    yeah, I realized what you had done with the <php> tags about 5 seconds after I posted…oops…

    Anyway, THANKS VERY MUCH – now if I can just get it to work with the future dates ie my other post…

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘get_posts WILL NOT WORK for CHILDREN! DAAGH!’ is closed to new replies.