2015-04-03 87 views
2

我想用標籤列表製作一個自定義模塊。當點擊一個標籤時,訪問者將被導航到一個類別頁面,該頁面將顯示帶有該標籤的文章。按類別標記過濾文章 - Joomla和K2

我不是一個Joomla專家,我在想喜歡這樣一個超級鏈接,我會添加到標籤模塊內部的解決方案:

href="http://mywebsite.com/index.php/itemlist/tag/tokio%20city?category=places" 

這可能嗎?或者我怎麼能達到這個結果? 謝謝!

回答

1

這比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將如何工作

我典型的類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將被打印

不過,這將是更加的方式代碼,我想保持簡單&可讀。我想我已經給了你足夠多的開始,所以繼續完成吧,祝你好運:)

+0

你真的很熟悉這兩個平臺!感謝這個驚人的答案,我想在提供反饋之前先使用它。你寫的代碼片段效果很好,我只有兩個問題:1.現在我的查詢在一列中顯示文章,我想這應該用「主要文章」來完成,但是沒關係,我想我可以稍後調整它在CSS中,還是應該使用輔助文章以及顯示結果? 2.是否有這些標準功能的文檔,所以我可以學到更多? :)再次感謝你,你真棒。 – 2015-04-06 06:53:21

+0

這就是我過去兩年一直在做的:) 您可以在K2類別設置中設置每個類別和每種文章(最新,主要,次要,鏈接)的列數,或者使用自定義CSS這兩個作品都很好:) 個人而言,我會建議你只使用一種類型的文章,讓我們說主要的,因爲使用更多的類型可能會導致你一些意想不到的麻煩。 我不認爲有這樣的文檔,它或多或少只是PHP和K2知識的一小部分。 – TeeJay 2015-04-06 10:58:59

+0

再次感謝! :) – 2015-04-06 11:02:15