extending "WordPress Category Posts" ? (4 posts)

  1. talgalili
    Posted 7 years ago #

    Hi people.
    this plugin allows a very cool feature (showing posts from the same category in the end of a post).
    But is lacking very basic features such as limiting the number of posts displayed, and raping the displayed headers with a ul/li tags.

    Any one knows enough php to know how to insert that into the code ?



  2. watershedstudio
    Posted 7 years ago #


    This is currently used as a very basic display of all posts in a specific category and it is not always ideal to limit the posts or force it into a list.

    To set a limit of returned posts you'd change the 'numberposts' value and change the sort order accordingly.

    To force it into a list you'd change the code to something like:

    echo '<ul>';
    foreach( (array) $posts as $post ) {
    	echo '<li><a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a><br /></li>';
    echo '</ul>';


  3. talgalili
    Posted 7 years ago #

    Thanks Brian, I understand.
    I was thinking of using the plugin for adding a list of "post on the same subject" at the end of posts.

    Thanks for the code :)


  4. Petter
    Posted 6 years ago #

    Hello! Made so it can instead print X random posts for any given category

    Plugin Name: WordPress Category Posts
    Plugin URI: http://watershedstudio.com/portfolio/software-development/wordpress-category-posts-plugin/
    Description: List the posts in a specific category, slightly edited by Hans Petter to instead print out a given number of random posts from given category
    Author: Watershed Studio, LLC
    Version: 2.0
    Author URI: http://watershedstudio.com/
    function wp_cat_posts( $catID = 0, $nposts = 10 ) {
            $catID = (int) $catID;
            $posts = get_posts(array('category' => $catID, 'numberposts' => -1, 'order' => ASC, 'orderby' => title));
            /*foreach( (array) $posts as $post ) {
                    echo '<a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a><br />';
            if ($nposts > count($posts)) $nposts = count($posts);
            foreach( array_rand($posts, $nposts) as $post ) {
                    echo '<li><a href="' . get_permalink($posts[$post]->ID) . '">' . $posts[$post]->post_title . '</a><br /></li>';

    Like that instead with two values passed to it instead of one, First one being the category. second one the number of random posts to appear for given category.
    very nice!

Topic Closed

This topic has been closed to new replies.

About this Topic