我试图实现的基本功能是获取当前帖子的标签,查找附加到这些标签的所有帖子,然后只返回至少有3个共同标签的帖子
我已经玩了一个星期左右了。
在这里找到了一个几乎完美完成工作的片段。
$tags = array( \'bread\', \'cheese\', \'milk\', \'butter\');
$args = array(
\'tag_slug__in\' => $tags
// Add other arguments here
);
// This query contains posts with at least one matching tag
$tagged_posts = new WP_Query( $args );
echo \'<ul>\';
while ( $tagged_posts->have_posts() ) : $tagged_posts->the_post();
// Check each single post for up to 3 matching tags and output <li>
$tag_count = 0;
$tag_min_match = 3;
foreach ( $tags as $tag ) {
if ( has_tag( $tag ) && $tag_count < $tag_min_match ) {
$tag_count ++;
}
}
if ( $tag_count == $tag_min_match ) {
//Echo list style here
echo \'<li><a href="\' . get_permalink() . \'" title="\' . get_the_title() . \'">\' . get_the_title() . \'</a></li>\';
}
endwhile;
wp_reset_query();
echo \'</ul>\';
但正如您所看到的,您必须手动将标记名输入到数组中。。。所以它不会自动工作。我尝试了几种不同的方法尝试自动从帖子中提取标签,但无法使其正常工作。。。
我的技能相当有限,只是在将标记id放入数组时,向错误的方向迈出了一些笨拙的步骤。
有人知道我如何才能做到这一点吗?
非常难堪。