0

我在我的主题 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>";


    }
4

1 回答 1

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>';
}

请注意,我已经对此进行了测试,但相信它应该可以工作......让我知道!

于 2013-01-05T02:14:41.393 回答