这比 URL 中的查询字符串要复杂一些,因为您还需要调整模板。
如果您想让它尽可能简单,我建议使用模板覆盖创建一个新的 K2 模板并编辑类别模板,以便它可以读取查询字符串参数并仅显示已按类别过滤的文章以及通过查询字符串的标签。
这只是一个简短的操作方法,现在有更多细节:
1) 使用模板覆盖创建新的 K2 模板。
在您的模板中,如果它不存在,请创建一个文件夹结构/templates/your_template/html/com_k2/templates/default。如果您想拥有更多 K2 模板,可以将“默认”替换为任何名称,但您必须手动将新模板设置为您拥有的每个类别。
现在从“/components/com_k2/templates/default”中获取内容并将其复制到模板中的新文件夹中。现在,K2 正在使用您的 /templates/your_template/html/com_k2/ 文件夹中的模板。如果您不了解模板覆盖,请随时 google 更多详细信息,这在自定义模板时非常重要。
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 是:
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
但这将是更多的代码,我想让它简单易读。我想我给了你足够的开始,所以继续完成它,祝你好运:)