0

So I made a simple php script to display the top 3 posts in a category as a list. Pretty simple. Thing is I'm going to be using it quite a bit so I thought I'd be cleaver and write it out as a function where I could just pass the category ID number and it would generate the list. I'm new to php but I've done this often enough in JavaScript.

Problem is it only seems to want to display the first category i passed to it. I've done some reading but neither wp_reset_postdata(); wp_reset_query() OR rewind_posts() seem to work.

This seems like something which should be fairly simple but I really don't know how to proceed. Any help is hugely appreciated.

Here is my code with all the extra bits cut out:

<!--PHP Slider Unordered List Generator-->
<?php
    function SliderBuilder($catagoryID) { 
        //Set up variables to convert catagoryID into useable string
        $preped_catagoryID = "'cat=".$catagoryID."&posts_per_page=3'";
        echo $preped_catagoryID;
        ?>
            <ul class="blog-slider" id="blog-slider">
        <?php
            $catquery = new WP_Query( $preped_catagoryID );
            while($catquery->have_posts()) : $catquery->the_post();
                ?>
                    <li class="blog-slide" onclick="location.href='<?php the_permalink();?>';">
                        <div class="blog-thumb">
                            <?php the_post_thumbnail('small-thumbnail'); ?>
                        </div>
                    </li>
                <?php
                echo $preped_catagoryID;
            endwhile;

            wp_reset_postdata();
        ?>
            </ul>
        <?php
    }
    SliderBuilder("2");
    SliderBuilder("3");
?>
0

If you switch to get_posts you don't have to worry about resetting the loop.

function SliderBuilder( $categoryID ) {

    $args = array (
            'cat'            => $categoryID,
            'posts_per_page' => 3,
    );

    $posts = get_posts( $args );

    echo $categoryID;

    ?>
    <ul class="blog-slider" id="blog-slider">
        <?php

        foreach ( $posts as $post ) { ?>

            <li class="blog-slide" onclick="location.href='<?php echo get_the_permalink( $post->ID ); ?>';">
                <div class="blog-thumb">
                    <?php echo get_the_post_thumbnail( $post->ID, 'small-thumbnail' ); ?>
                </div>
            </li>

            <?php
        }

        ?>
    </ul>
    <?php
}

SliderBuilder( 2 );
SliderBuilder( 3 );
  • 1
    Gosh you Stack-Angels never fail to amaze! Thanks a lot! – Robert Porter Oct 25 '16 at 6:45
  • If you have an extra minute, the get_the_excerpt(); function doesn't seem to work. I figure it's because it lacks a $post parameter. Is there a quick work around? – Robert Porter Oct 26 '16 at 12:14
  • You have access to the post object. Just get some of the data directly from $post->post_excerpt - codex.wordpress.org/Class_Reference/WP_Post -- or $excerpt = get_the_excerpt( $post ) – jgraup Oct 26 '16 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.