从函数返回结果

时间:2013-01-05 01:15:27

标签: php wordpress function for-loop echo

我在主题functions.php文件中在wordpress中创建了以下函数。我想在我的主题模板中调用此函数,并在我的主题模板中回显foreach部分。我到底该怎么做?我认为我必须从函数中返回一些东西,但我不知道如何设置它。非常感谢您的帮助。

    function geography_navigation($type, $field1, $field2, $field3, $field4, $field5) {

    global $wpdb;

    $results = $wpdb->get_results("SELECT DISTINCT wp_eva_geography.$field1, wp_eva_geography.$field2 
    FROM wp_eva_geography
    WHERE wp_eva_geography.$field3='$type' AND wp_eva_geography.$field4='$geo_no_dash';");

    echo "<ul>";

    foreach($results as $geography){

        echo "<li> <a href='/$type/$field5/{$geography->$field2}/'>{$geography->$field1}</a></li>";

    }
    echo "</ul>";


    }

1 个答案:

答案 0 :(得分:0)

将输出设置为变量并按如下方式返回:

function geography_navigation($type, $field1, $field2, $field3, $field4, $field5) 
{
    global $wpdb;

    $results = $wpdb->get_results("SELECT DISTINCT wp_eva_geography.$field1, wp_eva_geography.$field2
    FROM wp_eva_geography
    WHERE wp_eva_geography.$field3='$type' AND wp_eva_geography.$field4='$geo_no_dash';");

    $output = '<ul>';

    foreach($results as $geography)
    {
        $output .= "<li> <a href='/$type/$field5/{$geography->$field2}/'>{$geography->$field1}</a></li>";
    }

    return $output . '</ul>';
}

请注意我已对此进行过测试,但相信它应该有效......请告诉我!