2012-03-29 37 views
1

我非常感謝任何人的幫助。基本上我想創建一個由其他數組組成的數組,以解決wordpress的do_shortcode函數的侷限性。我正在使用一些函數來做這件事。從函數返回無序列表內容PHP

龍版的問題:

的代碼目前看起來是這樣的:

/* These are the functions contained in a functions file */ 

    function output_entry_data() { 
    $postdate = get_the_date('j/n/y'); 
    $entrytitle = get_the_title(); 

return ('<li><p class="postmeta">'. $postdate. '</p><h3>'. $entrytitle. '</h3></li>'); 
    } 



    function output_month_data($entrynomax = '', $month = '', $entrydata = '') { 
    $entryno = 1; 

    while($entryno <= $entrynomax) { 
    echo $entrydata[$entryno]; 
    $entryno++; 
    } 

    } 


    function output_year_data($monthlynomax = '', $year = '', $monthlydata = '') { 
    $monthno = 1; 

    while($monthno <= $monthnomax) { 
    echo do_shortcode('<h4>[slider title="'. $month. '"]</h4><ul>'. $monthlydata[$monthno]. '</ul>[/slider]'); 
    $monthno++; 
    } 

    } 

    /* This is from a loop that determines whether you have reached the end of a month or a year */ 

    $entrydata[$entryno] = output_entry_data(); 
    $entrynomax = $entryno; 

    $monthlydata = array($monthno => $monthno); 
    $monthlydata[$monthno] = return(output_month_data($entrynomax, $month, $entrydata)); 
    $monthlynomax = $monthno; 

    $annualdata[$yearno] = array($yearno => $yearno); 
    $annualdata[$yearno] = return(output_year_data($monthlynomax, $year, $monthlydata)); 

    $entryno = 1; 
    $monthno = 1; 
    $yearno++; 
    $yearo = get_the_date('Y'); 

    /* The idea is that all the data gets outputted at the end of the loop like this: */ 

    $yearnomax = $yearno; 

    echo ('<ul>'); 

    $yearno = 1; 

    if($yearno <= $yearnomax) { 
    echo do_shortcode('<h3>[expand title ="'. $year. '"]</h3><ul>'. $annualdata[$yearno]. '</ul>[/expand]'); 
    $yearno++; 
    } 

    echo('</ul>'); 

目前的代碼成功創建$ entrydata [$ entryno]數組,因爲函數output_entry_data()每次只返回一行代碼。

但是,當我嘗試爲每個月創建數組$ monthlydata [$ monthno]時,它只是運行output_month_data()函數並創建所有月度條目的大列表,而不是將數據傳遞給數組供其他功能使用。

我可以看到,這是因爲我應用於output_month_data在output_entry_data(「返回」)和「回聲」()

SHORT版本的問題

在數組$ entrydata每個項目的[$ entryno]是一個包含list item標籤的字符串,我希望output_monthly_data()返回$ entrydata [$ entryno]中所有項目的一個大字符串,以供其他函數使用,而不是像當前代碼那樣回顯它們。當涉及到一個while循環時,可以這樣做嗎?

非常感謝,我很欣賞這裏的任何輸入。

回答

0

是的,這是(很容易)可能的。至少有兩種方式

  1. 連接字符串,並返回結果字符串:在陣列

    function output_month_data($entrynomax = '', $month = '', $entrydata = '') { 
        $entryno = 1; 
    
        $return = ''; 
        while($entryno <= $entrynomax) { 
        $return .= $entrydata[$entryno]; # concatenate strings 
        $entryno++; 
        } 
    
        return $return; 
    } 
    
  2. 存儲結果,並使用implode返回包含所有項目的字符串:

    function output_month_data($entrynomax = '', $month = '', $entrydata = '') { 
        $entryno = 1; 
    
        $return = array(); 
        while($entryno <= $entrynomax) { 
        $return[] = $entrydata[$entryno]; # append to array 
        $entryno++; 
        } 
    
        return implode('', $return); # change '' to any glue you want 
    }