【问题标题】:K2 extra fields access in BT Content sliderBT 内容滑块中的 K2 额外字段访问
【发布时间】:2013-04-17 11:10:14
【问题描述】:

我正在尝试访问 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"]

【问题讨论】:

    标签: joomla joomla-k2


    【解决方案1】:

    在尝试访问值之前,首先将您的字符串解码为 json 对象。

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

    【讨论】:

    • 那是快速且完全准确的 :-) 谢谢!!
    【解决方案2】:

    好的,我让它按照我想要的方式工作。

    我想用我称为 'Accroche' 的额外字段替换介绍/全文。这个额外字段的 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
    

    如您所见,我已将介绍文本注释掉,因为我不想要它们。如果你想要 introtext 和 extrafield,你可以修改它。

    如果没有上面给出的 JSON 提示,我永远不会想到这一点。谢谢大家:)

    希望这会有所帮助。

    干杯!

    【讨论】:

      猜你喜欢
      • 2012-12-27
      • 2014-11-01
      • 2013-06-18
      • 2013-06-24
      • 2015-02-15
      • 2011-04-08
      • 2014-03-05
      • 2011-07-18
      • 2013-02-23
      相关资源
      最近更新 更多