WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WP Category Posts - Specify Amount (2 posts)

  1. krushinfo
    Member
    Posted 7 years ago #

    Hey,

    I'm using a great plugin called 'WP Category Posts' which allows me to put a categorys posts anywhere on my site. On specific pages or on the sidebar (with Exec-Php installed).

    I'm just wondering if its possible to limit the amount to 10 entries.. I just want a sidebar with the last 10 news stories from a specific category.

    Here is the code for the plugin..

    <?php
    /*
    Plugin Name: WP Category Posts
    Plugin URI: http://watershedstudio.com/portfolio/software/wp-category-posts.html
    Description: List the posts in a specific category
    Author: Brian Groce
    Version: 1.0
    Author URI: http://briangroce.com/
    */
    function wp_cat_posts( $catID ) {

    global $wpdb;
    $get_posts_in_cat = "SELECT $wpdb->posts.ID, $wpdb->posts.post_title, ";
    $get_posts_in_cat .= "$wpdb->post2cat.post_id, $wpdb->post2cat.category_id ";
    $get_posts_in_cat .= "FROM $wpdb->posts, $wpdb->post2cat ";
    $get_posts_in_cat .= "WHERE $wpdb->posts.ID = $wpdb->post2cat.post_ID ";
    $get_posts_in_cat .= "AND $wpdb->post2cat.category_id = '$catID' ";
    $get_posts_in_cat .= "AND $wpdb->posts.post_status = 'publish' ";
    $get_posts_in_cat .= "ORDER BY $wpdb->posts.post_title ";

    $get_posts_in_cat_result = mysql_query($get_posts_in_cat);
    while ($posts_in_cat_row = mysql_fetch_assoc($get_posts_in_cat_result)) {
    $post_title = $posts_in_cat_row['post_title'];
    $postID = $posts_in_cat_row['ID'];

    echo '<li><a href="' . get_permalink($postID) . '">' . $post_title . '</a><br /></li>';
    }
    }
    ?>

    And you call the category like this

    <?php wp_cat_posts(2); ?>

    With the number corresponding to the category you want listed.

    Any ideas?

  2. mtbello
    Member
    Posted 6 years ago #

    I just figured this out. In the php file, use the following

    ...
    $get_posts_in_cat = "SELECT $wpdb->posts.ID, $wpdb->posts.post_title, ";
    $get_posts_in_cat .= "$wpdb->post2cat.post_id, $wpdb->post2cat.category_id ";
    $get_posts_in_cat .= "FROM $wpdb->posts, $wpdb->post2cat ";
    $get_posts_in_cat .= "WHERE $wpdb->posts.ID = $wpdb->post2cat.post_ID ";
    $get_posts_in_cat .= "AND $wpdb->post2cat.category_id = '$catID' ";
    $get_posts_in_cat .= "AND $wpdb->posts.post_status = 'publish' ";
    $get_posts_in_cat .= "ORDER BY $wpdb->posts.post_date DESC ";
    $get_posts_in_cat .= "LIMIT 10";
    ...

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.