Wordpress post query php custom field conditional

Posted by Andy on Stack Overflow See other posts from Stack Overflow or by Andy
Published on 2010-05-12T18:14:10Z Indexed on 2010/05/12 18:14 UTC
Read the original article Hit count: 182

Filed under:

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; ?>

© Stack Overflow or respective owner

Related posts about Wordpress