2015-03-19 57 views
2

我正在嘗試在一篇文章下方顯示相關文章的類別。在WordPress中顯示相關文章的類別

我正在使用Newsmag主題。

是建立我的相關文章中的代碼是這樣的:

class td_module { 

    var $post; 
    var $title_attribute; 
    var $title; 
    var $href; 
    var $td_review; //review meta 
    var $category; 

    //constructor 

    function __construct($post) { 
     //this filter is used by td_unique_posts.php - to add unique posts to the array for the datasource 

     apply_filters("td_wp_boost_new_module", $post); 
     $this->post = $post; 
     $this->title = get_the_title($post->ID); 
     $this->title_attribute = esc_attr(strip_tags($this->title)); 
     $this->href = esc_url(get_permalink($post->ID)); 
     $this->category = ''; 

     if (has_post_thumbnail($this->post->ID)) { 
      $this->post_has_thumb = true; 
     } else { 
      $this->post_has_thumb = false; 
     } 

     //get the review metadata 
     $this->td_review = get_post_meta($this->post->ID, 'td_review', true); 
    } 

和顯示相關文章的部分是這樣的:

$buffy .= '<div class="td-module-thumb">'; 

       if (current_user_can('edit_posts')) { 
        $buffy .= '<a class="td-admin-edit" href="' . get_edit_post_link($this->post->ID) . '">edit</a>'; 
       } 

       $buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">'; 

        $buffy .= '<img width="' . $td_temp_image_url[1] . '" height="' . $td_temp_image_url[2] . '" itemprop="image" class="entry-thumb" src="' . $td_temp_image_url[0] . '" ' . $attachment_alt . $attachment_title . '/>'; 

         $buffy .= '<span class="td-module-thumb-category">'.$this->category.'</span>'; 
.................................... 

$this->category被我加入。我試圖從wp_terms表中獲取數據並顯示每個相關帖子的類別。 我是新來的WordPress(實際上,這是我第一次觸摸WordPress代碼)。

謝謝

回答

1

這應該與您現有的代碼工作:

$buffy .= '<div class="td-module-thumb">'; 
    $related_category = get_the_category($this->post->ID); 

    if (current_user_can('edit_posts')) { 
     $buffy .= '<a class="td-admin-edit" href="' . get_edit_post_link($this->post->ID) . '">edit</a>'; 
    } 

    $buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">'; 

     $buffy .= '<img width="' . $td_temp_image_url[1] . '" height="' . $td_temp_image_url[2] . '" itemprop="image" class="entry-thumb" src="' . $td_temp_image_url[0] . '" ' . $attachment_alt . $attachment_title . '/>'; 

      $buffy .= '<span class="td-module-thumb-category">'.$related_category[0]->cat_name.'</span>'; 

或者,如果你需要的類別是一個鏈接使用:

$buffy .= '<div class="td-module-thumb">'; 
    $related_category = get_the_category($this->post->ID); 

    if (current_user_can('edit_posts')) { 
     $buffy .= '<a class="td-admin-edit" href="' . get_edit_post_link($this->post->ID) . '">edit</a>'; 
    } 

    $buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">'; 

     $buffy .= '<img width="' . $td_temp_image_url[1] . '" height="' . $td_temp_image_url[2] . '" itemprop="image" class="entry-thumb" src="' . $td_temp_image_url[0] . '" ' . $attachment_alt . $attachment_title . '/>'; 

      $buffy .= '<span class="td-module-thumb-category"><a href="'.get_category_link($related_category[0]->term_id).'">'.$related_category[0]->cat_name.'</a></span>'; 

你可能會想要將$related_category = get_the_category($this->post->ID);到您的第一個粘貼代碼段中$this->category = '';