开发者

What is wrong with my PHP WordPress code?

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 )开发者_StackOverflow社区;
                    $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 -->


try putting the counter in while like this:

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

<?php
if ($counter % 2) {
                $specialprt = "";
            } else {
                $specialprt = "prt-right";
            }
 ?>


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜