2010-05-12 101 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; ?>