0

I'm trying to access the contents of a K2 extra field inside the BT content slider plugin. If I do

print_r($row->extra_fields);

I get

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

I need to access the value, but I've tried everything I could think of with no luck.

Tests I've done (also tried print_r for everything just in case):

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

2 に答える 2

3

値にアクセスする前に、まず文字列を json オブジェクトにデコードします。

<?php
$json = json_decode('[{"id":"16","value":"http:\/\/www.youblisher.com\/p\/611670-Test-   Intro-to-R\/"}]');
print_r($json[0]->value);
?>
于 2013-04-24T03:39:36.003 に答える
2

OK、思い通りに動作するようになりました。

イントロ/全文を「Accroche」と呼ばれる追加フィールドに置き換えたかったのです。この extrafield の ID は 132 です (以下のコードで使用される ID を知るのに役立ちます)。

2 つのファイルを編集します。

/modules/mod_bt_contentslider/classes/content.php および /modules/mod_bt_contentslider/classes/k2.php

最初に行うことは、データベースからエクストラフィールド情報を取得することです:

/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

ご覧のとおり、イントロ テキストは不要なのでコメント アウトしました。イントロテキストとエクストラフィールドの両方が必要な場合は、それを変更できます。

上記の JSON のヒントがなければ、これを理解することはできなかったでしょう。すべてに感謝します:)

お役に立てれば。

乾杯 !

于 2014-02-19T09:16:09.897 に答える