2017-06-11 99 views
1

我有以下功能:如何正確放置PHP呼應裏面的html span標籤

<?php 
function my_custom_address ($formats) { 
    $formats = array( 
     'default' => '<span class="my-address-country"></span> 
         <span class="my-address-city"></span>' 
    ); 
    return $formats; 
} 
add_filter('default_address_formats', 'my_custom_address', 15) ; 
?> 

而且我試圖把跨度標記中的以下的輸出:

$listing_address_country = get_my_field('address_country'); 
echo $listing_address_country; 

$listing_address_city = get_my_field('address_city'); 
echo $listing_address_city; 

最接近我想出了以下內容:

function my_custom_address ($formats) { 

    $listing_address_country = get_my_field('address_country'); 
    $listing_address_city = get_my_field('address_city'); 
    $formats = array( 
     'default' => echo '<span class="my-address-country">' . $listing_address_country . '</span>'; 
       echo '<span class="my-address-city">' . $listing_address_city . '</span>'; 
    ); 
    return $formats; 
} 
add_filter('default_address_formats', 'my_custom_listing_address', 15) ; 

但它仍然不正確。我知道我很接近,但我似乎仍然錯過了一些東西。

回答

2

在php函數中,你應該返回一個值並在調用它並將其值賦給一個變量或者在函數中回顯並且不返回時對其進行回顯。 不要在數組內回顯,你的代碼充滿了錯誤。

試試這個:

function my_custom_address() { 

    $listing_address_country = get_my_field('address_country'); 
    $listing_address_city = get_my_field('address_city'); 
    // Setting array values 
    $arrayValue='<span class="my-address-country">' . $listing_address_country . 
    '</span><span class="my-address-city">' . $listing_address_city . '</span>'; 
    $formats = array( 
     'default' =>$arrayValue 
    ); 
    return $formats; 
} 

// Now call your function 
$my_custom_address=my_custom_address(); // You can use it any were 
// To echo the return data do 
foreach($my_custom_address as $data){ 
echo $data; 
} 
+0

那偉大工程,非常感謝! –