WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] showing pages within posts (6 posts)

  1. Paul Jarvis
    Member
    Posted 8 years ago #

    i am putting a page list based on category onto posts, different page lists on different posts, which the code below does:


    <? if (is_page('7')) { ?>
    <? $posts = get_posts('order=DESC&category=1'); foreach ($posts as $post): ?>
    <? the_title(); ?>
    <? endforeach; ?>

    this however ONLY works if there are pages in that category, if there's no there's an error. how do i make it conditional to there being pages within the category? so if there are pages (one or more) then it shows them, if there's no pages posted in the category it posts nothing?

    any help would be appreciated.

  2. maerk
    Member
    Posted 8 years ago #

    You can't associate pages with categories, only posts...

  3. Paul Jarvis
    Member
    Posted 8 years ago #

    sorry, that's what i meant. it grabs posts in those categories from the get_posts. now i want it to only happen if there are posts in those categories. right now that get_posts returns an error if there are no posts in the category.

  4. maerk
    Member
    Posted 8 years ago #

    You might try changing

    <? if (is_page('7')) { ?>

    to

    <? if (is_page('7') && have_posts()) { ?>

  5. Paul Jarvis
    Member
    Posted 8 years ago #

    i still get a:

    Warning: Invalid argument supplied for foreach()

  6. Paul Jarvis
    Member
    Posted 8 years ago #

    i got it now:

    if i add an IF before the for statement.

    <? $posts = get_posts('order=DESC&category=1'); if ($posts) : foreach ($posts as $post): ?>

    thanks for your help!

Topic Closed

This topic has been closed to new replies.

About this Topic