2012-03-29 36 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 
    }