我想制作一个带有标签列表的自定义模块。当点击标签时,访问者将被导航到一个类别页面,该页面将显示带有该标签的文章。
我不是joomla专家,我正在考虑一个像这样的超链接的解决方案,我会将其添加到模块内的标签中:
href="http://mywebsite.com/index.php/itemlist/tag/tokio%20city?category=places"
这可能吗?或者我怎样才能达到这个结果?谢谢
这比URL中的查询字符串更复杂,因为您还需要调整模板。
如果你想让它尽可能简单,我建议你使用模板覆盖创建一个新的K2模板,并编辑类别模板,这样它就可以读取查询字符串参数,只显示已经通过类别和标签通过查询字符串过滤的文章。
这只是一个简单的操作方法,现在有更多的细节:
1)使用模板覆盖创建一个新的K2模板
在模板中,如果它还不存在,请创建一个文件夹结构/templates/your_template/html/com_k2/templates/default
。如果你想拥有更多的K2模板,"默认"可以替换为任何名称,但你必须手动将新模板设置为每个类别。
现在从"/contensions/com_k2/templates/default"中获取内容,并将其复制到模板中的新文件夹中。现在,K2正在使用/templates/your_template/html/com_k2/
文件夹中的模板。如果你不了解模板覆盖,可以随意搜索更多细节,这在自定义模板时非常重要。
2)编辑您的类别视图文件以将列表容纳到您的查询字符串
您现在感兴趣的文件位于/templates/your_template/html/com_k2/templates/default/category.php
中。打开这个文件并尝试了解其中的重要内容:
Line 141
<?php foreach($this->leading as $key=>$item): ?>
Line 169
<?php foreach($this->primary as $key=>$item): ?>
Line 197
<?php foreach($this->secondary as $key=>$item): ?>
Line 226
<?php foreach($this->links as $key=>$item): ?>
这才是最重要的。在这四个foreach循环中,存在所有项目。然后,您可以将每个循环的内容包装到if条件中,以检查它是否具有在URL中指定的所需标记。
为了向您展示一个示例,这是<div id="itemListPrimary">
的代码。您可以用以下代码替换category.php文件中的整个div,它将完美地工作。我刚刚写完并测试了它。
<div id="itemListPrimary">
<?php foreach ($this->primary as $key=>$item): ?>
<?php
# Get the value of the "tag" query string
$jInput = JFactory::getApplication()->input;
$myTag = $jInput->get('tag', null, 'STRING'); // Joomla 1.6+
//$myTag = JRequest::getVar('tag'); // for Joomla 1.5
# If the tag is empty, the query string is not specified and we'll go standard way without any tag filter
if (empty($myTag)) {
// Define a CSS class for the last container on each row
if ((($key+1)%($this->params->get('num_secondary_columns'))==0) || count($this->secondary)<$this->params->get('num_secondary_columns'))
$lastContainer= ' itemContainerLast';
else
$lastContainer='';
?>
<div class="itemContainer<?php echo $lastContainer; ?>"<?php echo (count($this->secondary)==1) ? '' : ' style="width:'.number_format(100/$this->params->get('num_secondary_columns'), 1).'%;"'; ?>>
<?php
// Load category_item.php by default
$this->item=$item;
echo $this->loadTemplate('item');
?>
</div>
<?php if(($key+1)%($this->params->get('num_secondary_columns'))==0): ?>
<div class="clr"></div>
<?php endif;
# Otherwise the tag is set so we'll filter the articles by the tag
} else {
# Get an array of all the tags that the current article in the loop has
$articleTags = array();
foreach ($item->tags as $tag) {
$articleTags[] = $tag->name;
}
# Check if the article has the tag specified in the URL as a query string
if (in_array($myTag, $articleTags)) {
# Now the default content of the foreach loop comes as written in the default K2 category.php template
// Define a CSS class for the last container on each row
if ((($key+1)%($this->params->get('num_secondary_columns'))==0) || count($this->secondary)<$this->params->get('num_secondary_columns'))
$lastContainer= ' itemContainerLast';
else
$lastContainer='';
?>
<div class="itemContainer<?php echo $lastContainer; ?>"<?php echo (count($this->secondary)==1) ? '' : ' style="width:'.number_format(100/$this->params->get('num_secondary_columns'), 1).'%;"'; ?>>
<?php
// Load category_item.php by default
$this->item=$item;
echo $this->loadTemplate('item');
?>
</div>
<?php if(($key+1)%($this->params->get('num_secondary_columns'))==0): ?>
<div class="clr"></div>
<?php endif;
}
} ?>
<?php endforeach; ?>
</div>
3)了解URL的工作方式
我的典型类别URL是:
http://mywebsite.com/category-name
要只显示带有指定标签的文章,请使用:
http://mywebsite.com/category-name?tag=your-tag
例如,如果您想只显示带有"Tokio City"标签的文章,请使用:
http://mywebsite.com/category-name?tag=Tokio City
完成
这是你所需要的基础。如果你只使用初级文章(没有前导和辅助或链接),这就是你所需要的。当然,你可能还需要处理更多的事情:
- 如果没有带有指定标签的文章,则会发出通知
- 没有多余的代码,为了简单易读,我这样写
- SEO-URL中的空格和特殊字符
- 确保不会打印空div
但这将是更多的代码,我想保持它的简单&您可以阅读。我想我给了你足够的开始时间,所以继续做吧,祝你好运:)