2015-04-01 177 views
0

我正在使用大量標籤和每個帖子分類的音樂網站。例如,在藝術家的頁面上,相關的帖子顯示基於標籤的藝術家的發佈。我曾嘗試添加使用Wordpress' post_type的$ args =陣列(標籤 -根據標籤顯示相關帖子

'post_type' => 'releases' 

,但並未奏效

例如,下面是完整的代碼 -

<div class="relatedposts"> 
      <h3>Releases by Artist</h3> 
      <?php 
       $orig_post = $post; 
       global $post; 
       $tags = wp_get_post_tags($post->ID); 

       if ($tags) { 
       $tag_ids = array(); 
       foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; 
       $args=array(
       'post_type' => 'releases', 
       'tag__in' => $tag_ids, 
       'post__not_in' => array($post->ID), 
       'posts_per_page'=>4, // Number of related posts to display. 
       'caller_get_posts'=>1 
       ); 

       $my_query = new wp_query($args); 

       while($my_query->have_posts()) { 
       $my_query->the_post(); 
       ?> 

       <div class="relatedthumb"> 
        <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br /> 
        <?php the_title(); ?> 
        </a> 
       </div> 

       <? } 
       } 
       $post = $orig_post; 
       wp_reset_query(); 
       ?> 
      </div> 

我跟着其他很多帖子#1的,似乎無法得到正確的結果。我在哪裏去了?!

回答

0
<section class="row related"> 
    <div class="row related-articles"> 
     <h2 class="related-title">Related Content</h2> 
     <div class="relatedposts"> 
      <?php 
     $orig_post = $post; 
     global $post; 
     $tags = wp_get_post_tags($post->ID); 

     if ($tags) { 
      $tag_ids = array(); 
     foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; 
      $args=array(
       'tag__in' => $tag_ids, 
       'post__not_in' => array($post->ID), 
       'posts_per_page'=>3, // Number of related posts to display. 
       'caller_get_posts'=>1 
      ); 

     $my_query = new wp_query($args); 

     while($my_query->have_posts()) { 
      $my_query->the_post(); 
     ?> 
      <div class="large-4 medium-4 small-12 columns img-wrap"> <a rel="external" href="<? the_permalink()?>"> 
       <?php $url = wp_get_attachment_url(get_post_thumbnail_id($post->ID, 'thumbnail')); ?> 
       <img src="<?php echo $url ?>" /> 
       <div class="story-title-sub"> 
        <h2> 
         <?php the_title(); ?> 
        </h2> 
       </div> 
      </div> 
      <?php } 
     } 
     $post = $orig_post; 
     wp_reset_query(); 
     ?> 
     </div> 
    </div> 
</section> 
+0

沒有,似乎並沒有被它的相關... – 2015-04-02 12:54:23

+0

我編輯了以上我的網站上使用的代碼,它看起來像相同的腳本,它在我的網站上工作。看起來你唯一真正不同的地方是你正在調用帖子類型。看看,也許你可以在上面找到答案 – 2015-04-02 13:12:31

+0

是的,這是我使用的代碼。它調用所有相關的標籤。我已經在$ args = array中嘗試了很多變體(section。嗯。令人費解的。 – 2015-04-02 13:18:51

0

你可以嘗試讓這個職位代碼由標籤

<?php // related posts based on first tag of current post 
$tags = wp_get_post_tags($post->ID); 
if ($tags) { 

    echo '<h3>Related Posts</h3>'; 

    $first_tag = $tags[0]->term_id; 
    $args = array(
      'tag__in' => array($first_tag), 
      'post__not_in' => array($post->ID), 
      'showposts' => 7, // how many posts? 
      'caller_get_posts' => 1 
      ); 

    $my_query = new WP_Query($args); 
    if ($my_query->have_posts()) { ?> 

     <ul> 

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

      <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 

     <?php endwhile; ?> 

     </ul> 

    <?php } ?> 
<?php } ?> 
相關問題