Forums

[resolved] Category Posts Plugin (6 posts)

  1. mtbello
    Member
    Posted 5 years ago #

    I'm using just upgraded to WordPress 2.2. I'm attempting to use WordPress Category Posts v1.0 Plugin (http://blog.watershedstudio.com/2005/05/wordpress-category-posts-plugin-v10/). The plugin works great, but I cannot limit the number of posts without getting errors.

    The php reads:

    <?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_date DESC";
    $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 '' . $post_title . '';
    }
    }

    ?>

    If I add $get_posts_in_cat .= "LIMIT 10"; to limit the number of posts displayed, I get error messages. Can anyone see the error in my ways?

  2. MichaelH
    Volunteer
    Posted 5 years ago #

    Maybe you'd have better luck with the Custom Query String plugin.

  3. mtbello
    Member
    Posted 5 years ago #

    Thanks for the suggestion MichaelH, but that really doesn't work for me.

    I'm trying to create two separate boxes on my sidebar, each one containing recent posts titles from different categories. Basically, one box is men's scores and the other box is women's scores from a sports league.

    In the sidebar I use the line <?php wp_cat_posts(1); ?> and <?php wp_cat_posts(2); ?>. This works great for my purposes. However, the list will grow forever. I need a way to limit the number of posts.

    My error may be in the location of the $get_posts_in_cat .= "LIMIT 10";

    Any thoughts? I really appreciate the help.

  4. mtbello
    Member
    Posted 5 years ago #

    Well, trying again this morning it worked. Thank you for your help.

  5. MichaelH
    Volunteer
    Posted 5 years ago #

    Is it something as simple as a space problem?

    $get_posts_in_cat .= "LIMIT 10";

    should be

    $get_posts_in_cat .= " LIMIT 10";

  6. jcaudill
    Member
    Posted 4 years ago #

    I've updated the code for WP 2.0+ and to support limiting, and CSS class.

    Put the following in a file, name it wp_category_posts.php and put it in your plugins folder. Activate and all should be right in the world.

    <?php
    /*
    Plugin Name: WP Category Posts
    Plugin URI: http://www.itsensellc.com
    Description: List the posts in a specific category - for version 2.0+ only - support optional arguments of limit and css class
    Author: JP Caudill
    Version: 1.0
    Author URI: http://www.itsensellc.com
    */ 
    
    function wp_cat_posts( $catID, $limit=null, $class=null ) {
    
    	global $wpdb;
    
    		$get_posts_in_cat = "SELECT ID,post_title ";
    		$get_posts_in_cat .= "FROM $wpdb->posts ";
    		$get_posts_in_cat .= "WHERE ID IN (SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$catID') ";
    		$get_posts_in_cat .= "AND post_type = 'post' ";
    		$get_posts_in_cat .= "ORDER BY post_date DESC ";
    
    		if($limit)
    			$get_posts_in_cat .= "LIMIT 0, $limit ";
    
    		$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 '<a href="' . get_permalink($postID) . '" class="' . $class .  '">' . $post_title . '</a><br />';
    		}
    }
    
    ?>

    Use it like:

    <?php wp_cat_posts(3); ?> - straight links, no styling and no limit
    <?php wp_cat_posts(3,10); ?> - straight links, no styling limited to 10
    <?php wp_cat_posts(3,10,'leftmenu'); ?> - links with a css class of leftmenu appled and limited to 10

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.