2012-07-28 137 views
1

我做了搜索不僅在谷歌,但其他地方,包括這裏,並找不到任何東西來幫助解決這個問題。WordPress相關帖子錯誤

這是問題所在。我有一個基於標籤而不是類別的相關帖子片段,我在WordPress主題中使用它,並且我已經使用了它一段時間,並且它工作得很好。這裏是:

 $tags = wp_get_post_tags($post->ID); 
    $tagIDs = array(); 
    if ($tags) { 
     $tagcount = count($tags); 
     for ($i = 0; $i < $tagcount; $i++) { 
      $tagIDs[$i] = $tags[$i]->term_id; 
     } 
    $args=array(
    'tag__in' => $tagIDs, 
    'post__not_in' => array($post->ID), 
    'showposts'=>mytheme_option('related_count'), 
    'ignore_sticky_posts'=>1 
    ); 
    $my_query = new WP_Query($args); 
    if($my_query->have_posts()) { 
     echo '<h4>'. __('Other Posts You May Be Interested In', "themename"). ':</h4><ul>'; 
     while ($my_query->have_posts()) : $my_query->the_post(); ?> 

      <li class="imglink"> 

       <!-- post loop stuff goes here --> 

      </li> 

<?php endwhile; 
echo '</ul>'; 
} 
else { 
echo '<h4>'. __('Other Posts You May Be Interested In', "themename"). ':</h4> 
'. __('<p>There are no related posts at this time.</p>', "themename"). ''; 
} 
} 
$post = $original_post; 
wp_reset_query(); 

就像我說的那樣,它工作得很好。如果職位有相同的標籤,那麼這表明:

其他職位,你可能感興趣的: 帖子同一個標籤的GET顯示

但這裏的問題是:如果一個職位是給定一個標籤,沒有其他職位有相同的標籤,這是表明:你可能有興趣在

其他職位:有在 沒有相關的帖子。

現在,如果帖子未分配標籤,則完全不顯示。相關帖子應該顯示的div是空的,但應該說沒有相關的帖子。

我已經找了一個解決方案,並嘗試了很多不同的事情,以得到這個糾正,但我似乎無法得到我的頭。有人可以幫助我得到:

您可能感興趣的其他職位:目前沒有相關文章 這次。

顯示帖子是否沒有標籤。任何幫助非常感謝,並非常感謝你提前。

回答

0

我認爲,問題來自於條件對函數的頂部:

$tags = wp_get_post_tags($post->ID); 

$tagIDs = array(); 
if ($tags) { 
    # All of your code is in this conditional, 
    # if there are no tags, nothing will happen 
} 

如果發現該職位沒有標籤,它會跳過所有的代碼,並不會呈現任何內容。代碼底部的else語句僅在當前帖子有標籤時執行,但其他帖子不包含該標籤。

編輯以實例的要求 - 你需要有呼應了沒有帖子內容2個其他條件,一是因爲如果沒有標籤和一個是否有標籤,但職位均未發現:

$tags = wp_get_post_tags($post->ID); 

$tagIDs = array(); 
if (count($tags) > 0) { 
    // setup $tagIDs and perform query... 

    if($my_query->have_posts()) { 
     // Echo out posts. 
    } else { 
     echo '<h4>'. __('Other Posts You May Be Interested In', "themename"). ':</h4> 
     '. __('<p>There are no related posts at this time.</p>', "themename"). ''; 
    } 
} else { 
    echo '<h4>'. __('Other Posts You May Be Interested In', "themename"). ':</h4> 
    '. __('<p>There are no related posts at this time.</p>', "themename"). ''; 
} 

這應該做到這一點。不過,我可能會建議將內容封裝到一個函數中。然後,您可以在該函數結尾回覆無帖子內容,如果發現帖子,則返回'返回'。那麼你不會重複自己。

+0

這就是我的想法。我會建議在最後添加一個額外的else語句來顯示錯誤。 – leejmurphy 2012-07-28 19:09:39

+0

感謝球員們,我想到了同樣的事情,並試圖在最後添加額外的聲明,但我不能讓它工作。你可以幫助一個可能的例子,在最後會顯示我需要的條件。再次感謝您的幫助。 – tsquez 2012-07-28 19:14:49

+0

用示例編輯。 – jhummel 2012-07-28 19:29:29