0

I am trying to build a custom view of categories, I want to have all 'post's show their thumbnail within a jQuery carosel (4 in each slide) , I have got it to a stage where it visually looks how I want it to look, but it's showing ALL posts rather then the category your on, I have a big feeling this is due to my custom loop oposed the the standard one (I am using twentyten as my base) any ideas what I am doing wrong?

Also on a side note, cat.php (at the bottom) is made by manually adding a post call, with a post offset for each new item added, is there a better way of doing this so I don't need to manually add a case for each post I want to display?

category.php...

    <div id="container">
            <div id="content" role="main">
                <div style="position: relative; margin:0 auto; width: 960px; height:500px;">
<!-- "previous page" action -->
<a class="prev browse left"></a>

<!-- root element for scrollable -->
<div class="scrollable" id="scrollable">

  <!-- root element for the items -->
  <div class="items">

    <?php
                    $category_description = category_description();
                    if ( ! empty( $category_description ) )
                        echo '<div class="archive-meta">' . $category_description . '</div>';

                /* Run the loop for the category page to output the posts.
                 * If you want to overload this in a child theme then include a file
                 * called loop-category.php and that will be used instead.
                 */
                get_template_part( 'cat' );
                ?>


</div>

<!-- "next page" action -->
<a class="next browse right"></a>
</div>
            </div><!-- #content -->
        </div><!-- #container -->

cat.php (my custom loop)

<div>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php
global $post;
$args = array( 'numberposts' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
                <span id="post-<?php the_ID(); ?>" <?php post_class(); ?>>


            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                <?php the_post_thumbnail('large');?>
            </a>
        </span>

<?php endforeach; ?>
<?php endwhile; // end of the loop. ?>

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php
global $post;
$args = array( 'numberposts' => 1, 'offset'=> 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
                <span id="post-<?php the_ID(); ?>" <?php post_class(); ?>>


            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                <?php the_post_thumbnail('large');?>
            </a>
        </span>

<?php endforeach; ?>
<?php endwhile; // end of the loop. ?>


<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php
global $post;
$args = array( 'numberposts' => 1, 'offset'=> 2 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
                <span id="post-<?php the_ID(); ?>" <?php post_class(); ?>>


            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                <?php the_post_thumbnail('large');?>
            </a>
        </span>

<?php endforeach; ?>
<?php endwhile; // end of the loop. ?>
</div>

<div>


<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php
global $post;
$args = array( 'numberposts' => 1, 'offset'=> 3 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
                <span id="post-<?php the_ID(); ?>" <?php post_class(); ?>>


            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                <?php the_post_thumbnail('large');?>
            </a>
        </span>

<?php endforeach; ?>
<?php endwhile; // end of the loop. ?>


<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php
global $post;
$args = array( 'numberposts' => 1, 'offset'=> 4 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
                <span id="post-<?php the_ID(); ?>" <?php post_class(); ?>>


            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                <?php the_post_thumbnail('large');?>
            </a>
        </span>

<?php endforeach; ?>
<?php endwhile; // end of the loop. ?>


<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php
global $post;
$args = array( 'numberposts' => 1, 'offset'=> 5 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
                <span id="post-<?php the_ID(); ?>" <?php post_class(); ?>>


            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                <?php the_post_thumbnail('large');?>
            </a>
        </span>

<?php endforeach; ?>
<?php endwhile; // end of the loop. ?>

</div>

2 Answers 2

0

Why are you running a complete new query for every single post?

Here is what is happening:

  1. WordPress gets all the posts in the category then gets your template and sees the loop ( if have_posts() etc... ) and says ok I have all these posts from this category now it's time to display them.

  2. Bam HALT!!! Ok I have these posts but here is another query: get_posts and it is asking me for 1 post but what about all these posts I already have? He must not want them so I will get this one post he wants.

  3. Ok I'm displaying the post he wants just 1 and since no other options were specified I will just grab the most recent post in the database

This repeats for each new query and loop.

WordPress already knows the category you want there is no need to run the get_posts foreach loop with the offsets.

just run:

 <?php   if( have_posts() ) : while (have_posts() ) : the_post(); ?>
    
       <span id="post-<?php the_ID(); ?>" <?php post_class(); ?>>


            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                <?php the_post_thumbnail('large');?>
            </a>
        </span>

    
  <?php  endwhile; endif; ?>

And WordPress will return the number of posts set in your posts per page options settings from what ever category it gets from the url. No need to create a complete new query for each and every post.

2
  • I'm not sure if this is correct but your story was brilliant, and I kinda get it. Wil run test quickly! May 23, 2012 at 1:14
  • Sorry dosent exactly do what I want, I have some divs, i want 3 posts in each div, do fist div will have post 1,2,3 2nd div will have 4,5,6 etc, then it would just loop. I'm creating them manualy but trying to find a way for it to be generated. May 23, 2012 at 1:18
0

Rename cat.php to loop-cat.php and then do,

get_template_part( 'loop', 'cat' );

See if that helps.

Also don't really understand what you are trying to do with the multiple loops? Since you are within the category view for a specific category name, is it not more logical to show 4, 5, 6 etc posts using one loop? Thus the loop will output the most recent x-amount of posts you specify in descending order (or ascending if you so choose). I say this because you are only returning 1 post per loop then off setting the next loop which is equal to doing 'numberposts' => 6

1
  • Hi, Thanks, I tried to add 'get_template_part( 'loop', 'cat' );' but it still is the same, and pulls in ALL posts, the the category I am on. Your correct, I have added number posts, so now I have two loops, each with 3 posts and the 2nd one has an offset of 3 but is there a way to automatically group posts into 3's so they can be contained in a div (for a slider)? May 21, 2012 at 14:08

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.