WordPress.org

Support

Support » Plugins and Hacks » limiting by cat in php loop

limiting by cat in php loop

  • 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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Help? Please?

    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;
    }
    ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘limiting by cat in php loop’ is closed to new replies.
Skip to toolbar