我有多个帖子,其中一些具有相同的标签,我只需要显示每个标签的第一篇文章。我无法在Google或此处找到解决方案。
Wordpress Codex以显示每个标签的示例为例。这可能与查询帖子结合吗
<?php $tags = get_tags();
$html = '<div class="post_tags">';
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html; ?>
只有您必须这样做,只需在每个标签循环内调用具有特定帖子标签的帖子查询,如下所示-
$tags = get_tags();
$html = '<div class="post_tags">';
foreach ( $tags as $tag ) :
$tag_link = get_tag_link( $tag->term_id );
$html .= '<a href="'.$tag_link.'" title="'.$tag->name.' Tag" class="'.$tag->slug.'">'.$tag->name.'</a>';
// fetch tag specific post
$args = array(
'numberposts' => 1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $tag->slug
)
)
);
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
$html .= '<h2><a href="'.get_the_permalink( $post->ID ).'">'.$post->post_title.'</a></h2>';
$html .= $post->post_content;
endforeach;
endforeach;
$html .= '</div>';
echo $html;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句