1

I'm trying to modify the Genesis Tab plugin to show more than one post per tab. I've tried in vain for the past hour, but my little PHP knowledge just isn't cutting it unfortunately. Here is the current code, that should be returning 5 post titles ('posts_per_page' => 5), but is only showing one:

// Loop through all chosen categories
        foreach ( (array) $cats as $cat ) :

            if ( ! $cat ) continue; // skip iteration if $cat is empty

            // Custom loop
            $tabbed_posts = new WP_Query( array( 'cat' => $cat, 'posts_per_page' => 5, 'orderby' => 'date', 'order' => 'DESC' ) );

            if ( $tabbed_posts->have_posts() ) : while ( $tabbed_posts->have_posts() ) : $tabbed_posts->the_post();

                echo '<div id="cat-' . $cat . '" '; post_class( 'ui-tabs-hide' ); echo '>';

                    if( ! empty( $instance['show_title'] ) ) :
                        printf( '<h2><a href="%s" title="%s">%s</a></h2>', get_permalink(), the_title_attribute( 'echo=0' ), get_the_title() );
                    endif;


                echo '</div><!--end post_class()-->'."\n\n";

            endwhile; endif;

        endforeach;

I'm guessing it's something to do with the have_posts() being within the if() statement, but I could be entirely wrong. Is somebody able to point me in the right direction? Searching here has helped me swap out some depreciated tags that were in the plugin for more recent versions, but unfortunately I haven't been able to solve the 'real' issue at hand.

Thanks!

Jamie

3
  • Have you tried doing a var_dump of the $tabbed_posts before the if?
    – Tomás Cot
    Commented Aug 23, 2014 at 20:13
  • Thanks. I've just tried this and it does seem to output all posts that it should. So am I right in thinking this does point to the if() statement causing the issue? While I can see what the var_dump proves (it's returning all posts, then stripping them to just show the last) I'm still not sure how to go about fixing it. Thanks for your help so far!
    – Jamie H
    Commented Aug 23, 2014 at 20:47
  • Hace you checked the source of the page? MAybe what is failing is the if of the $instance var.
    – Tomás Cot
    Commented Aug 23, 2014 at 21:02

2 Answers 2

1

Looks like you might be missing the wp_reset_postdata() in here. The WordPress codex shows you need to have a wp_reset_postdata() after your while loop.

Here's WordPress' example (which even has the posts_per_page defined):

<?php
// example args
$args = array( 'posts_per_page' => 3 );

// the query
$the_query = new WP_Query( $args );
?>

<?php if ( $the_query->have_posts() ) : ?>

    <!-- start of the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <?php the_title(); ?>
        <?php the_excerpt(); ?>
    <?php endwhile; ?><!-- end of the loop -->

    <!-- put pagination functions here -->
    <?php wp_reset_postdata(); ?>

<?php else:  ?>

<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>

<?php endif; ?>

http://codex.wordpress.org/Function_Reference/wp_reset_postdata

1
  • This does not really answer the question, but you are correct on wp_reset_postdata +1 Commented Aug 24, 2014 at 8:05
0

Thanks all. After an overnight break, I was able to find a simple solution to this...

The script was inserting a hide class on to all other posts, apart from the first. I don't know how I didn't spot this last night. Removingpost_class( 'ui-tabs-hide' ); from the script has fixed it.

I also added the wp_reset_postdata() as suggested by Pat.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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