0
我對相關的文章shortcode使用了bellow函數,我對此有疑問。它應該顯示與相關標籤的帖子(我不是爲什麼它只顯示隨機帖子),但我想用[rps tag = google]之類的東西,而函數只是返回一個帖子有一個標籤爲「谷歌」,我的意思是與當前帖子標籤無關。我怎樣才能做到這一點?
這是代碼:
wp中的相關文章的簡碼
add_shortcode('rps', 'fphp_get_related_posts');
function fphp_get_related_posts() {
$reset_post = $post;
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
$post_tag_ids = array();
foreach($post_tags as $post_tag) $post_tag_ids[] = $post_tag->term_id;
$args=array(
'tag__in' => $post_tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page' => 1,
'orderby' => 'rand',
);
$related_query = new wp_query($args);
if (intval($related_query->post_count) === 0) return '';
$html = '<div class="rps"><ul><h3>Also read:</h3>';
$related_query->the_post();
$html .= '<li style="width:250px">';
$html .= '<div class="relatedthumb"><a rel="external" href="'. get_the_permalink(). '">';
$html .= get_the_title() . '</a>';
$html .= '</div></li>';
}
$post = $reset_post;
wp_reset_query();
$html .= '</ul></div>';
return $html;
}
非常好。謝謝。 – ata