2010-05-12 107 views
0

这里的情况:WordPress的职位查询PHP的自定义字段条件

在WordPress我想重置后WP_Query,这样我可以重写基于岗位是否存在一个自定义字段的帖子链接。我试图在自定义字段中提供一个新链接。

我设法在这里完成的是完全杀死链接。任何和所有的帮助非常感谢,我对PHP很绿。

这里是我的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; ?> 

回答

0

我觉得这是你所需要的。很难说。我想if语句的第一部分是什么运行,如果没有自定义后期元?我说不出来。这是问题所在。 if语句运行第一部分,如果有一个返回的自定义帖子元的值,否则它运行第二部分,使用空字符串作为href。 (如果自定义值不存在或者只是空字符串,则运行第一部分)。更改if语句以检查它是否为空更好,因为如果它不存在(返回false),或者它确实存在但是一个空字符串(已声明但未定义),则会捕获它。

我已经标记了我所编辑的评论(只有一行)。

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