Wordpress函数:无法从数组中获取某些值

时间:2014-12-12 20:37:09

标签: php arrays wordpress nextgen-gallery

我正在从NextGen Gallery Wordpress插件修改一个类。最后,我试图通过组装NGG存储在数据库中的必要位来构建图像的URL。
我已经达到了原始代码执行SQL查询并提供数组然后从该数组中提取某些值的程度。该数组包含我需要的所有值,但我似乎只能得到一些值,我无法弄清楚原因。

这是我目前修改的代码:

//  Class copied from ngg plugin, change class name from 'nggWidget' to 'nggHeaderBG'
    class nggHeaderBG extends WP_Widget {
        function widget( $args, $instance ) {
           global $wpdb;
           $items   = $instance['items'];
           $exclude = $instance['exclude'];
           $list = $instance['list'];
                   $exclude_list = "AND t.gid IN ($list)";
           $imageList = $wpdb->get_results("SELECT t.*, tt.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE tt.exclude != 1 $exclude_list ORDER by rand() limit {$items}");
           foreach($imageList as $image) {
                // get the URL constructor
                $theimage = new nggImage($image);

                // Just create the image URL
                $out .= ''.home_url().'/'.$theimage->path.'/'.$theimage->filename;
                echo $out;
        }
    }

在上面的代码中,$theimage->filename有效,但$theimage->path没有。 var_dump($theimage)生成以下数组(它很长,所以我将它存储在代码段中):



object(nggImage)#3716 (2) {
  ["_ngiw"]=>
  object(C_Image_Wrapper)#3717 (8) {
    ["_cache"]=>
    array(34) {
      ["alttext"]=>
      string(9) "homepage3"
      ["author"]=>
      string(2) "10"
      ["description"]=>
      string(1) " "
      ["errmsg"]=>
      string(0) ""
      ["error"]=>
      bool(false)
      ["exclude"]=>
      string(1) "0"
      ["filename"]=>
      string(13) "homepage3.gif"
      ["galdesc"]=>
      string(72) "For Home Landing Page Only.
Must be 880 pixels wide by 350 pixels high."
      ["galleryid"]=>
      string(1) "2"
      ["gid"]=>
      string(1) "2"
      ["hidden"]=>
      bool(false)
      ["href"]=>
      string(0) ""
      ["imagePath"]=>
      string(0) ""
      ["imageURL"]=>
      string(0) ""
      ["image_slug"]=>
      string(9) "homepage3"
      ["imagedate"]=>
      string(19) "2014-10-01 16:03:54"
      ["meta_data"]=>
      array(17) {
        ["width"]=>
        int(880)
        ["height"]=>
        int(350)
        ["full"]=>
        array(2) {
          ["width"]=>
          int(880)
          ["height"]=>
          int(350)
        }
        ["thumbnail"]=>
        array(4) {
          ["width"]=>
          int(350)
          ["height"]=>
          int(139)
          ["filename"]=>
          string(20) "thumbs_homepage3.gif"
          ["generated"]=>
          string(21) "0.25234600 1412179434"
        }
        ["aperture"]=>
        bool(false)
        ["credit"]=>
        bool(false)
        ["camera"]=>
        bool(false)
        ["caption"]=>
        bool(false)
        ["created_timestamp"]=>
        bool(false)
        ["copyright"]=>
        bool(false)
        ["focal_length"]=>
        bool(false)
        ["iso"]=>
        bool(false)
        ["shutter_speed"]=>
        bool(false)
        ["flash"]=>
        bool(false)
        ["title"]=>
        bool(false)
        ["keywords"]=>
        bool(false)
        ["saved"]=>
        bool(true)
      }
      ["name"]=>
      string(14) "second_gallery"
      ["pageid"]=>
      string(1) "0"
      ["path"]=>
      string(31) "wp-content/gallery/home_headers"
      ["permalink"]=>
      string(0) ""
      ["pid"]=>
      string(3) "411"
      ["post_id"]=>
      string(1) "0"
      ["previewpic"]=>
      string(3) "403"
      ["slug"]=>
      string(12) "home_headers"
      ["sortorder"]=>
      string(1) "0"
      ["style"]=>
      string(0) ""
      ["tags"]=>
      string(0) ""
      ["thumbFolder"]=>
      string(8) "/thumbs/"
      ["thumbPath"]=>
      string(0) ""
      ["thumbPrefix"]=>
      string(7) "thumbs_"
      ["thumbURL"]=>
      string(0) ""
      ["thumbcode"]=>
      string(0) ""
      ["title"]=>
      string(12) "Home_Headers"
    }
    ["_settings"]=>
    NULL
    ["_storage"]=>
    NULL
    ["_galleries"]=>
    NULL
    ["_orig_image"]=>
    object(stdClass)#3715 (20) {
      ["gid"]=>
      string(1) "2"
      ["name"]=>
      string(14) "second_gallery"
      ["slug"]=>
      string(12) "home_headers"
      ["path"]=>
      string(31) "wp-content/gallery/home_headers"
      ["title"]=>
      string(12) "Home_Headers"
      ["galdesc"]=>
      string(72) "For Home Landing Page Only.
Must be 880 pixels wide by 350 pixels high."
      ["pageid"]=>
      string(1) "0"
      ["previewpic"]=>
      string(3) "403"
      ["author"]=>
      string(2) "10"
      ["pid"]=>
      string(3) "411"
      ["image_slug"]=>
      string(9) "homepage3"
      ["post_id"]=>
      string(1) "0"
      ["galleryid"]=>
      string(1) "2"
      ["filename"]=>
      string(13) "homepage3.gif"
      ["description"]=>
      string(1) " "
      ["alttext"]=>
      string(9) "homepage3"
      ["imagedate"]=>
      string(19) "2014-10-01 16:03:54"
      ["exclude"]=>
      string(1) "0"
      ["sortorder"]=>
      string(1) "0"
      ["meta_data"]=>
      array(17) {
        ["width"]=>
        int(880)
        ["height"]=>
        int(350)
        ["full"]=>
        array(2) {
          ["width"]=>
          int(880)
          ["height"]=>
          int(350)
        }
        ["thumbnail"]=>
        array(4) {
          ["width"]=>
          int(350)
          ["height"]=>
          int(139)
          ["filename"]=>
          string(20) "thumbs_homepage3.gif"
          ["generated"]=>
          string(21) "0.25234600 1412179434"
        }
        ["aperture"]=>
        bool(false)
        ["credit"]=>
        bool(false)
        ["camera"]=>
        bool(false)
        ["caption"]=>
        bool(false)
        ["created_timestamp"]=>
        bool(false)
        ["copyright"]=>
        bool(false)
        ["focal_length"]=>
        bool(false)
        ["iso"]=>
        bool(false)
        ["shutter_speed"]=>
        bool(false)
        ["flash"]=>
        bool(false)
        ["title"]=>
        bool(false)
        ["keywords"]=>
        bool(false)
        ["saved"]=>
        bool(true)
      }
    }
    ["_orig_image_id"]=>
    string(3) "411"
    ["_cache_overrides"]=>
    NULL
    ["_legacy"]=>
    bool(true)
  }
  ["_propogate"]=>
  bool(true)
}




我可以获得一些变量,但不能获得其他变量。除了文件名之外,我还可以获得' alttext',' pid',' imagedate'' image_slug' ,例如,' thumbFolder',' thumbPrefix'一切都有效。 其他人,比如'路径'和' slug',我可以用它来构建我的网址,总是带着价值' second_gallery'这是'名称'值。它似乎是任何无法提取的值的默认回退。我甚至试过$theimage->_orig_image->path,但这没有产生任何结果。使用方括号方法,即$theimage['path']只会导致整个网站陷入困境。 任何人都可以解释为什么可以获得某些值,但其他值不能?而且,更重要的是,如何从这个特定的数组中获取我想要的值(主要是'路径')? 感谢。

1 个答案:

答案 0 :(得分:0)

它是数组和对象的混合但忽略了缓存数组,这可能就是你要找的东西:

$path= $theimage->_ngiw->_orig_image->path;