tags:

views:

38

answers:

1

Here's the situation:

In wordpress I'm trying to reset a post WP_Query so that I can rewrite the post link based on whether or not a custom field exists in the post. I'm trying to give the post a NEW link in the custom field.

All I've managed to do here is kill the link entirely. Any and all help is greatly appreciated, I'm pretty green to php.

Here's my WP_Query:

  <?php
                     $recentPosts = new WP_Query();
   $recentPosts->query('showposts=3');
  ?>

                    <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>

                     <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
   <?php
    $attribute = the_title_attribute();
    $title = the_title();
    $key = 'NewPostLink';
    $newLink = get_post_meta( $post->ID, $key, TRUE );
    if ($newLink != '') {
     $theLink = get_permalink ($post->ID );
     if (has_post_thumbnail()) {
      $image = get_the_post_thumbnail( $post->ID );
      echo '<div class="thumbnailbox"><div class="thumbnail"><a href="'.$theLink.'">'.$image.'</a></div></div>';
      echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
     } else {
      echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
     }
    } else {
     $theLink = $newLink;
     if (has_post_thumbnail()) {
      $image = get_the_post_thumbnail( $post->ID );
      echo '<div class="thumbnailbox"><div class="thumbnail"><a href="'.$theLink.'">'.$image.'</a></div></div>';
      echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
     } else {
      echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
     }
    }
   ?>
                     <small><?php the_time('F jS, Y') ?></small>

                      <div class="entry">
                      <?php the_excerpt(); ?>
                     </div>

                     </div>

                     <?php endwhile; ?>
A: 

I think this is what you need. It's hard to tell. I suppose that the first part of the if statement is what runs if there is no custom post meta? I couldn't tell. Here's what the problem was. The if statement ran the first part if there IS a value returned for the custom post meta, otherwise it ran the second part, using the empty string as the href. (The first part runs if the custom value either doesn't exist or is anything but an empty string). Changing the if statement to check if it's empty is better because it will catch it if it doesn't exist (returns false), or if it does exist but is an empty string (declared but not defined).

I've marked what I edited with comments (just one line).

    <?php
                         $recentPosts = new WP_Query();
       $recentPosts->query('showposts=3');
      ?>

                        <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>

                         <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
       <?php
        $attribute = the_title_attribute();
        $title = the_title();
        $key = 'NewPostLink';
        $newLink = get_post_meta( $post->ID, $key, TRUE );
/* EDITED */        if (empty($newLink)) {
         $theLink = get_permalink ($post->ID );
         if (has_post_thumbnail()) {
          $image = get_the_post_thumbnail( $post->ID );
          echo '<div class="thumbnailbox"><div class="thumbnail"><a href="'.$theLink.'">'.$image.'</a></div></div>';
          echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
         } else {
          echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
         }
        } else {
         $theLink = $newLink;
         if (has_post_thumbnail()) {
          $image = get_the_post_thumbnail( $post->ID );
          echo '<div class="thumbnailbox"><div class="thumbnail"><a href="'.$theLink.'">'.$image.'</a></div></div>';
          echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
         } else {
          echo '<h2><a href="'.$theLink.'" rel="bookmark" title="Permanent Link to '.$attribute.'">'.$title.'</a></h2>';
         }
        }
       ?>
                         <small><?php the_time('F jS, Y') ?></small>

                          <div class="entry">
                          <?php the_excerpt(); ?>
                         </div>

                         </div>

                         <?php endwhile; ?>
John P Bloch