nurain
nurain

Reputation: 195

Can't get this overly complicated loop to work correctly

So I'm editing the index.php in my WP theme's folder, essentially what I want it to do is this:

Here's the loop I'm working with, the first page appears fine, but for some reason on page 2 and higher it's executing really weird and showing only one post on each additional page. Also, it will only show the title and not the date or excerpt. Here's my code:

           <?php 
            if( is_home() && !is_paged() ){
            global $query_string;
            parse_str( $query_string, $args );
            $args['posts_per_page'] = 4;
            query_posts($args);
            if (have_posts()) :
                while (have_posts()) : the_post();
           if (++$counter == 1) { ?>
            <div class="featured_post">
                <p class="date"><?php the_date('M j, Y'); ?></p>
                <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('featuredblog'); ?></a>
                <?php the_excerpt(); ?>
            </div>
          <?php } else { ?>
            <div class="regular_post">
                <p class="date"><?php the_date('M j, Y'); ?></p>
                <div class="postimage"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('regularblog'); ?></a></div>
                <a href="<?php the_permalink(); ?>"><h3><?php
                    // short_title($after, $length)
                    echo short_title('...', 7);
                    ?></h3></a>
                <?php the_excerpt(); ?>
            </div>
          <?php } ?>
        <?php endwhile;
        else :
           // Code for no posts found here
        endif;
        } else {
            global $query_string;
            parse_str( $query_string, $args );
            $args['posts_per_page'] = 6;
            query_posts($args); ?>
            <div class="regular_post">
                <p class="date"><?php the_date('M j, Y'); ?></p>
                <div class="postimage"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('regularblog'); ?></a></div>
                <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
                <?php the_excerpt(); ?>
            </div>
        <?php } ?>

Maybe there's too many if...else statements in there?

Upvotes: 0

Views: 207

Answers (3)

nurain
nurain

Reputation: 195

<?php if (is_home() && !is_paged() ) {
                global $query_string;
                parse_str( $query_string, $args );
                $args['posts_per_page'] = 4;
                query_posts($args);
                } else { 
                global $query_string;
                parse_str( $query_string, $args );
                $args['posts_per_page'] = 6;
                query_posts($args);
                } ?>
            <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
            <div class="post<?php echo ++$counter; ?>">
                    <?php if ( is_home() && !is_paged() && $counter == 1 ) { ?>
                        do stuff
                    <?php } else { ?>
                        do other stuff
                    <?php } ?> 
            </div>
            <?php endwhile; ?>

Upvotes: 0

nathan gonzalez
nathan gonzalez

Reputation: 12017

my suggestion would follow yes123, but really leave it all to css. basically, all of your posts should be wrapped in a parent div, and each should be classed the same way.

<div id="content">
    <div class="post">
        <div class="title"></div>
    </div>
    <div class="post">
        <div class="title"></div>
    </div>
    <div class="post">
        <div class="title"></div>
    </div>
    <div class="post">
        <div class="title"></div>
    </div>
</div>

that's about what you should resolve to, with more tags in the post, obviously.

from there, the way to style the first post differently would be using the #content > .post:first-child selector.

also, having a different number of posts per page is probably not a good idea. i've actually never really tried it, but i can see where the paging would get screwed up, and you'd probably never see posts 5 or 6. i'm thinking wordpress would probably say that with 6 posts per page, page 2 should always start with post 7. not positive though.

Upvotes: 1

dynamic
dynamic

Reputation: 48141

Are you using some kind of framework? that's a pretty bad tempalte engine.

Anyway if yuo need to add style, you should leave the job to CSS.

if you need extremly precision you can just leave a class with a counter for each element you need to style differently.

like <div class="Element<?php echo ++$counter; ?>"></div>

and then you can add your styles in CSS

.Element1,.Element2,.Element3 {styles}
.Element4 {other styles}

in this way you simplify the life with your php code

Keep in mind one day you will not even need to add a class counter.. In the near future you could use the nth-child css selector http://reference.sitepoint.com/css/pseudoclass-nthchild

Edit: damn me I replied someone with 50% AR

Upvotes: 1

Related Questions