0

This is my code in index.php of the WordPress theme:

<div id="content">
            <?php if (have_posts()) : ?>
            <?php $counter = "0"; ?>
            <?php while (have_posts()) : the_post(); ?>
            <?php
                if ($counter % 2) {
                    $specialprt = "";
                } else {
                    $specialprt = "prt-right";
                }
            ?>
            <div class="partial <?php echo $specialprt; ?>" id="post-<?php the_ID(); ?>">
                <div class="prt-img">
                    <?php echo bdw_get_images($post->the_ID, 'medium'); ?>
                </div>
                <div class="prt-tags">
                    <?php the_tags(' ', ''); ?>
                </div>
                <h2 class="prt-title">
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
                </h2>
                <span class="prt-small">Posted on <?php the_time('l F jS') ?> by <?php the_author() ?></span>
                <p><?php the_excerpt(); ?></p>
                <p><?php edit_post_link('Edit', '', ''); ?></p>
            </div>
            <?php $counter++; ?>
            <?php endwhile; ?>
            <?php endif; ?>
        </div><!-- #content -->

Problems:

  1. All the posts have the same image. Why?
  2. Page loads very slow. Why?

Can anybody give a hand of help? :)

Thanks.

UPDATE:

I have better results with this:

<div id="content">
            <?php if (have_posts()) : ?>
            <?php $counter = "0"; ?>
            <?php while (have_posts()) : the_post(); $counter++; ?>
            <?php
                if ($counter % 2) {
                    $specialprt = "prt-right";
                } else {
                    $specialprt = "";
                }
            ?>
            <div class="partial <?php echo $specialprt; ?>" id="post-<?php the_ID(); ?>">
                <div class="prt-img">
                    <?php
                    $args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $post->ID );
                    $attachments = get_posts($args);
                    if ($attachments) {
                        foreach ( $attachments as $attachment ) {
                            the_attachment_link( $attachment->ID , true, false, false );
                        }
                    } else {
                        echo "<img src=\"<?php bloginfo('stylesheet_directory'); ?>/images/no-image.jpg\" width=\"250\" height=\"155\" />";
                    }
                    ?>
                </div>
                <div class="prt-tags">
                    <?php the_tags(' ', ''); ?>
                </div>
                <h2 class="prt-title">
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
                </h2>
                <span class="prt-small">Posted on <?php the_time('l F jS') ?> by <?php the_author() ?></span>
                <p><?php the_excerpt(); ?></p>
                <p><?php edit_post_link('Edit', '', ''); ?></p>
            </div>
            <?php endwhile; ?>
            <?php endif; ?>
        </div><!-- #content -->
4
  • Where does $post come from? Commented Jun 2, 2011 at 7:11
  • @deceze: WordPress makes and populates this global (!) variable after the_post(); is called, as part of the Loop. Commented Jun 2, 2011 at 7:13
  • @deceze: Oh and welcome to the 50k-rep club! Commented Jun 2, 2011 at 7:18
  • @Bolt I see. Ugh, Wordpress. And thanks. :o) Commented Jun 2, 2011 at 7:21

3 Answers 3

1

try putting the counter in while like this:

<?php while (have_posts()) : the_post();$counter++;?>

<?php
if ($counter % 2) {
                $specialprt = "";
            } else {
                $specialprt = "prt-right";
            }
 ?>
Sign up to request clarification or add additional context in comments.

1 Comment

I changed to this. It seems now the page is loading much faster. Hmmm... Can't see yet the sense but thank you very much man! :)
1

How many images is


echo "hello world";

returning? If it is loading in a lot of images, or just a few large ones that could be the cause of your page loading slowly.

1 Comment

That test echo has returned 10 times, as many times as many articles I have called on the landing page. I think there is a problem with the id's of them, not the number of images returned. Not sure. Thanks.
1

I can answer your first question:

  1. $post->the_ID is incorrect. Although it isn't recognized as part of the $post object, PHP keeps notices quiet so you don't see any error happening. Instead, bdw_get_images() just receives a null ID and produces the same "image" for every post.

    The correct value to pass in is either $post->ID or calling get_the_ID().

But not the second one, as I don't think there's enough information in your post to start figuring out what's slowing things down. Try perhaps using your browser's web development tools to analyze the request and response to see what's going on.

1 Comment

I've changed, but nothing happened. bdw_get_images() is a function I found on the internet to extract attachment images in WordPress. Thanks.