2013-04-24 71 views
0

我試圖訪問BT內容滑塊插件內K2額外字段的內容。如果我做在BT內容滑塊中的K2額外字段訪問

print_r($row->extra_fields); 

我得到

[{"id":"16","value":"http:\/\/www.youblisher.com\/p\/611670-Test-Intro-to-R\/"}] 

我需要訪問的價值,但我已經嘗試了所有我能沒有運氣想到的。

測試中,我已經做了(還試圖print_r的一切以防萬一):

echo $row->extra_fields[0] 
echo $row->extra_fields[0]->value 
echo $row->extra_fields->value 
echo $row->extra_fields["value"] 

回答

3

首先試圖訪問前值解碼你的字符串轉換成JSON對象。

<?php 
$json = json_decode('[{"id":"16","value":"http:\/\/www.youblisher.com\/p\/611670-Test- Intro-to-R\/"}]'); 
print_r($json[0]->value); 
?> 
+0

這是快速和完全準確的:-)謝謝! – 2013-04-24 03:45:05

2

好吧,我按照自己想要的方式工作。

我想用一個叫做'Accroche'的外場替換intro/full text。該字段的ID爲132(有用於瞭解將在下面的代碼中使用的ID)。

我們將編輯兩個文件:

/modules/mod_bt_contentslider/classes/content.php 和 /modules/mod_bt_contentslider/classes/k2.php

首先要做的就是讓extrafield從數據庫信息:

在/modules/mod_bt_contentslider/classes/content.php(圍繞線77)我加入並[b] a.extra_fields,[/ b]如下

$model->setState('list.select', 'a.urls, a.images, a.fulltext, a.id, a.title, a.alias, a.introtext, a.extra_fields, a.state, a.catid, a.created, a.created_by, a.created_by_alias,' . ' a.modified, a.modified_by,a.publish_up, a.publish_down, a.attribs, a.metadata, a.metakey, a.metadesc, a.access,' . ' a.hits, a.featured,' . ' LENGTH(a.fulltext) AS readmore');  

保存文件&接近

現在讓我們去/modules/mod_bt_contentslider/classes/k2.php(約234線),

替換此原代碼

// cut introtext 
     if ($limitDescriptionBy == 'word') { 

      $item->description = self::substrword($item->introtext, $maxDesciption, $replacer, $isStrips, $stringtags); 

      $item->description = self::substring($item->introtext, $maxDesciption, $replacer, $isStrips, $stringtags); 

     } 
     $item->categoryLink = urldecode(JRoute::_(K2HelperRoute::getCategoryRoute($item->catid . ':' . urlencode($item->categoryalias)))); 

// get author name & link 

有了這個代碼,我已經評論過讓像我這樣的新手可以理解的東西;)

 // REPLACE intro/full text With extra-field info 
     $extras = json_decode($item->extra_fields); // JSON Array we'll call extras (note final 's' : not to confuse with below variable) 
     foreach ($extras as $key=>$extraField): //Get values from array 
      if($extraField->value != ''): //If not empty 
        if($extraField->id == '132'): // This is ID value for extrafield I want to show --- Search your K2 extrafield's id in Joomla backoffice ->K2 ->extrafields --- 
         if($extraField->value != ''): // If there's content in the extrafield of that ID 
          $extra = $extraField->value; //Give $extra that value so we can hand it down below 
         endif; 
        endif; 
      endif; 
     endforeach; 

     // cut introtext 
     if ($limitDescriptionBy == 'word') { 

      // $item->description = self::substrword($item->introtext, $maxDesciption, $replacer, $isStrips, $stringtags); 
      $item->description = self::substrword($extra, $maxDesciption, $replacer, $isStrips, $stringtags); 
     } else { 

      // $item->description = self::substring($item->introtext, $maxDesciption, $replacer, $isStrips, $stringtags); 
      $item->description = self::substring($extra, $maxDesciption, $replacer, $isStrips, $stringtags) ; 

     } 
     $item->categoryLink = urldecode(JRoute::_(K2HelperRoute::getCategoryRoute($item->catid . ':' . urlencode($item->categoryalias)))); 

     // get author name & link 

正如你所看到的,因爲我不想要它們,所以列出了介紹文本。如果你想同時使用introtext和extrafield,你可以修改它。

如果沒有上面給出的JSON提示,我從來沒有想過這個。感謝所有:)

希望這會有所幫助。

乾杯!