Viewing 3 replies - 1 through 3 (of 3 total)
  • Tal,

    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>';

    Brian

    Thread Starter talgalili

    (@talgalili)

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

    Tal.

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

    <?php
    /*
    Plugin Name: WordPress Category Posts
    Plugin URI: https://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: https://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!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘extending “WordPress Category Posts” ?’ is closed to new replies.