WordPress.org

Ready to get started?Download WordPress

Forums

limiting by cat in php loop (3 posts)

  1. wordie
    Member
    Posted 7 years ago #

    I know this has been addressed but I'm just not getting it. On the index php page for the site is this:

    <?php
    $how_many=5; //How many posts do you want to show
    require_once("blog/wp-config.php"); // Change this for your path to wp-config.php file ?>

    <?
    $news=$wpdb->get_results("SELECT ID,post_title FROM $wpdb->posts
    WHERE post_status= \"publish\" ORDER BY 'ID' DESC LIMIT ".$how_many);
    foreach($news as $np){
    printf ("%s<br><br>", $np->ID,$np->post_title);
    }?>

    It works fine, pulling in the first 5 posts. However I need to filter out anything that is NOT in category 1.

    All the info I can find shows completely different php, none of which I can get to work. Is there some way to just make what I have do what I need?

    Thanks.

  2. wordie
    Member
    Posted 7 years ago #

    Help? Please?

  3. wordie
    Member
    Posted 7 years ago #

    Figured it out. For future reference:

    <?php
    $how_many=5; //How many posts do you want to show
    $category=1; //Category you want to show
    require_once("blog/wp-config.php"); // Change this for your path to wp-config.php file ?>

    <?php
    query_posts("cat={$category}&showposts={$how_many}");
    if(have_posts()) { while(have_posts()) : the_post();
    echo "" . get_the_title() . "<BR><BR>";
    endwhile;
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.