我正在尝试在一篇文章下方显示相关帖子的类别。
我正在使用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代码)。
谢谢
答案 0 :(得分: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 = '';
的位置