我希望重新创建通常在博客中看到的标签云侧边栏,在博客中用户在标签上进行选择,它会弹出具有该特定标签的所有帖子。
现在,我在确定如何设置查询时遇到了麻烦。
我有查询来拉所有标签:
$blogTags = $this->createQueryBuilder('b')
->select('b.tags')
->getQuery()
->getResult();
return $blogTags;
但是,如何将其设置为仅拉出用户从侧边栏中的一组标签中选择的该标签的帖子呢?
我有在侧边栏中存储标签并对其进行加权的代码,我正在寻找将标签链接到其特定帖子的下一步。
getsTags()
public function getTags()
{
$blogTags = $this->createQueryBuilder('b')
->select('b.tags')
->getQuery()
->getResult();
$tags = array();
foreach ($blogTags as $blogTag) {
$tags = array_merge(explode(",", $blogTag['tags']), $tags);
}
foreach ($tags as $tag) {
$tag = trim($tag);
}
return $tags;
}
getTagWeights()
public function getTagWeights($tags)
{
$tagWeights = array();
if (empty($tags))
return $tagWeights;
foreach ($tags as $tag)
{
$tagWeights[$tag] = (isset($tagWeights[$tag])) ? $tagWeights[$tag] + 1 : 1;
}
// Shuffle the tags
uksort($tagWeights, function() {
return rand() > rand();
});
$max = max($tagWeights);
// Max of 5 weights
$multiplier = ($max > 5) ? 5 / $max : 1;
foreach ($tagWeights as &$tag)
{
$tag = ceil($tag * $multiplier);
}
return $tagWeights;
}