Symfony2-如何列出一个特定的post标签而不是所有标签?
在这个问题上难倒了。我想链接我的帖子标签,这样当它被点击时,它会列出该标签的所有帖子,类似于标签云——将该功能添加到帖子下的各个标签 目前,我已经将其设置为使用与标记云相同的功能,但当我将鼠标悬停在一个标记上时,它会显示所有标记,因为我在for循环中使用了blog结果(blog.tags)。请参阅屏幕截图:(将鼠标悬停在一个标记上会显示博客文章中的所有标记) 当我使用“获取所有标签”和“for”循环遍历这些标签时(当我选择某个特定标签时,会按标签为我提供所有帖子),但它也会列出所有标签,而不是我不想要的特定于帖子的标签。(标记存储为字符串)请参见屏幕中的 如何将其设置为只显示悬停在上面的特定标签,而不是帖子中的所有标签? 小树枝Symfony2-如何列出一个特定的post标签而不是所有标签?,symfony,tags,twig,Symfony,Tags,Twig,在这个问题上难倒了。我想链接我的帖子标签,这样当它被点击时,它会列出该标签的所有帖子,类似于标签云——将该功能添加到帖子下的各个标签 目前,我已经将其设置为使用与标记云相同的功能,但当我将鼠标悬停在一个标记上时,它会显示所有标记,因为我在for循环中使用了blog结果(blog.tags)。请参阅屏幕截图:(将鼠标悬停在一个标记上会显示博客文章中的所有标记) 当我使用“获取所有标签”和“for”循环遍历这些标签时(当我选择某个特定标签时,会按标签为我提供所有帖子),但它也会列出所有标签,而不是
{% for blog in pagination %}
<p>Tags: <span class="highlight"><a href="{{ path('AcmeDemoBundle_tag', { 'tag': blog.tags }) }}">{{ blog.tags }}</a></span></p><br><br>
{% endfor %}
注意到您使用的代码: {%用于分页%中的日志} 标签:{{blog.Tags}
{%endfor%} 这不是推荐的方法 正确的方法是必须使用嵌套循环,并且标记链接应显示如下内容: tag1 tag2 将所有标记存储在一个字段中,并用逗号或其他符号分隔,这绝不是一个好主意: 第1册| tag1、tag2、tag3 但建议这样做: 第1册第1页 第2册第2页
希望这有帮助 谢谢Taylor,谢谢你的推荐,设置并不理想,但我仍处于学习阶段(没有借口,我知道),谢谢你给我展示了一个更好的方法。
public function indexAction($tag = null)
{
// Search function using code from Services/Search.php
$query = $this->get('search');
$results = $query->search();
$em = $this->getDoctrine()->getManager();
$blogs = $em->getRepository('AcmeDemoBundle:Blog')
->getBlogs();
// Get all tags
$tags = $em->getRepository('AcmeDemoBundle:Blog')
->getTags();
// Get all posts by tag
$postTags = $em->getRepository('AcmeDemoBundle:Blog')
->getPostsByTags($tag);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$blogs,
$this->get('request')->query->get('page', 1)/*page number*/,
5/*limit per page*/
);
return array(
'blogs' => $blogs,
'query' => $query,
'results' => $results,
'tags' => $tags,
'postTags' => $postTags,
'pagination' => $pagination,
);
}
public function tagAction($tag = null)
{
$em = $this->getDoctrine()->getManager();
$tags = $em->getRepository('AcmeDemoBundle:Blog')
->getPostsByTags($tag);
if (!$tags) {
throw $this->createNotFoundException('Unable to find blog posts');
}
return array(
'tags' => $tags,
);
}