2011-03-23 44 views
0

下面的腳本旨在掃描名爲「widgets」的文件夾以查找其子文件夾及其包含的.txt文件。每個子文件夾都是我主題註冊的「側邊欄小部件」之一的代理,每個文件夾中的.txt文件都是「文本小部件」,它們將插入到其父文件夾所代表的已註冊側欄中。幫助增加for循環中的對象索引(它只寫入文件夾中的最後一個對象)

該腳本正常工作,將單個文本小部件放到每個註冊的側欄中。但是,當一個文件夾包含多個文本文件時,它不會根據需要遞增widget_id值,唯一被寫入邊欄的文本小部件是該文件夾中的最後一個。

如何在這種情況下爲widget_id設置適當的計數器?

/*Install Widgets from .txt files in child folders of "/widgets/" 
-------------------------------------*/ 

$sidebars_widgets = get_option('sidebars_widgets'); 
$widget_ops = get_option('widget_text'); 
$widget_id = count($widget_ops)+1; 


$base = dirname(__FILE__).'/widgets/'; 
$rdi = new RecursiveDirectoryIterator($base); 
foreach(new RecursiveIteratorIterator($rdi) as $files_widgets) 
    { 
    if ($files_widgets->isFile()) 
     { 
      $file_name_widget = $files_widgets->getPathname(); 
      $sidebar_id = basename($files_widgets->getPath()); 
      $widget_text = file_get_contents($file_name_widget); 
      $sidebars_widgets[$sidebar_id] = array("text-".$widget_id); 
    //Do I need another loop here? 
    //Only the last widget in the folder is created 
      $widget_ops[$widget_id] = array('title' => $files_widgets->getBasename('.txt'),'text' => $widget_text,); 
      update_option('widget_text', $widget_ops); 
      update_option('sidebars_widgets', $sidebars_widgets); 
      $widget_id = $widget_id +1; 
     } 
    } 

背景: 的「小工具」文件夾包含5個文件夾,每個代表的註冊欄,我的主題創建並命名爲側邊欄的ID。

例如,該工具條是註冊在functions.php的

register_sidebar(array(
    'name' => 'Home Sidebar', 
    'id' => 'home-sidebar-widget', 
    'before_widget' => '<div class="menu side %2$s sb">', 
    'after_widget' => '</div>', 
    'before_title' => '<h4 class="sidebarTitle">', 
    'after_title' => '</h4>', 
)); 

以及包含所有將預填充的是小部件是小部件的相應文件夾...

widgets/home-sidebar-widget/

而且它包含3個.txt文件,每個文件都代表應該添加到該側欄的文本小部件。

問題是,我的計數器widget_id顯然是不正確的,我只是得到每個文件夾中寫入側邊欄的最後一個.txt文件。

+0

你的代碼看起來不錯,但我不知道是什麼的'update_option()'函數做 – 2011-03-23 04:10:40

+0

它的內置函數爲WordPress。它只是更新widget_text的值,並將$ widget_ops中的數組傳遞給數據庫。代碼工作正常,它只是覆蓋文件夾中的每個文件,直到它到達最後一個。這是唯一寫入數據庫的。這讓我覺得我錯過了一個櫃檯,或者我需要另一個循環。 – 2011-03-23 04:22:21

+0

那麼在那種情況下,我猜'update_option()'會用你傳遞它的值更新數據庫中名爲'widget_text'的選項。所以在每次迭代中,相同的選項正在被更新並被下一個覆蓋。因此只有最後一個保存在數據庫中。我想只有一個唯一的選項'widget_text'和'sidebars_widgets'。讓我知道如果我錯了 – 2011-03-23 04:28:35

回答

1

你可以嘗試類似的東西,假設數組在序列化之前被保存在數據庫中,然後在檢索時被反序列化。

嘗試類似的東西

/*Install Widgets from .txt files in child folders of "/widgets/" 
-------------------------------------*/ 

// Remove the line below as we will fetch the value in the loop 
//$sidebars_widgets = get_option('sidebars_widgets'); 

$widget_ops = get_option('widget_text', array()); 
$widget_id = count($widget_ops) + 1; 

$base = dirname(__FILE__).'/widgets/'; 
$rdi = new RecursiveDirectoryIterator($base); 
foreach(new RecursiveIteratorIterator($rdi) as $files_widgets) 
{ 
    if ($files_widgets->isFile()) 
    { 
      $file_name_widget = $files_widgets->getPathname(); 
      $sidebar_id = basename($files_widgets->getPath()); 
      $widget_text = file_get_contents($file_name_widget); 

      // Retrieve the last value of the 'sidebars_widgets' option 
      $sidebars_widgets = get_option('sidebars_widgets', array()); 

      // Add the current widget to the sidebar 
      $sidebars_widgets[$sidebar_id][] = "text-".$widget_id; 

      // Retrieve the last value of the 'widget_text' option 
      $widget_ops = get_option('widget_text', array()); 

      // Add the new widget to the list of widgets 
      $widget_ops[$widget_id] = array('title' => $files_widgets->getBasename('.txt'),'text' => $widget_text,); 

      // Update the options with the updated arrays 
      update_option('widget_text', $widget_ops); 
      update_option('sidebars_widgets', $sidebars_widgets); 
      $widget_id = $widget_id + 1; 
    } 
} 

編輯
- 由+運營商更換array_merge()

+0

@Sly,我嘗試了兩種方式:直接(原樣)和您的[$ sidebar_id] []建議。後者拋出一個錯誤(strip_tags()期望參數1爲字符串...)...繼續... – 2011-03-23 12:29:36

+0

... as版本不會錯誤,但它也不能正常工作。它的正確之處在於,它確實將位於其中一個文件夾中的所有小部件放置到小部件中。但是,它會將它們寫入錯誤的側邊欄。它將一個小部件放到每個側邊欄中,但它們應該全部位於相同的側邊欄中(因爲它們位於相同的文件夾中)。 – 2011-03-23 12:35:59

+0

@Scott是的,我明白。嘗試替換'$ sidebars_widgets [$ sidebar_id] [] = array(「text - 」。$ widget_id);'by'$ sidebars_widgets [$ sidebar_id] [] =「text - 」。$ widget_id;'。我已經相應地更新了我的答案代碼。讓我知道這是否有效。 – 2011-03-23 16:38:32