2012-10-12 53 views
0

我有一個php implode函數來列出WordPress的自定義字段鍵,這很好用。問題是我需要在一個href中包裝網址,以使它們可點擊,並且無法將我的頭包裹在函數sytnax中。鍵值是這種格式www.site.com,所以我有它設置了像這樣一個條目:圍繞implode變量包裝href

<?php if(get_post_meta($post->ID, 'website', true)): ?> 
    <strong>Website:</strong> <a href="http://<?php echo get_post_meta($post->ID, 'website', true); ?>" target="_blank"><?php echo get_post_meta($post->ID, 'website', true); ?></a> 
<?php endif; ?> 

,但現在我們需要的是能夠容納用逗號分隔的多個條目。這裏是工作的代碼,但不輸出一個可點擊的網址:

<?php 
if($website = get_post_meta($post->ID, 'website')): 
    $label = count($website) > 1 ? 'Websites' : 'Website'; 
    ?> 
    <strong><?php echo $label; ?>:</strong> <?php echo implode($website, ', '); ?><br /> 
    <?php 
endif; 
?> 

這是我一直在玩,這顯然是錯誤的

<?php echo '<a href="http://' . implode('" target="_blank">', $website) . "</a>" . ', '; ?><br /> 

即使工作,它只會輸出url,而不是鏈接的文本。

---------------------編輯------------------------ -

Kai的答案是最接近的,所以我標記了答案,但它沒有包含標籤變量。嫁給他們兩個我想出了這個答案,精美的作品

<?php 
    if($website = get_post_meta($post->ID, 'website')): 
     $label = count($website) > 1 ? 'Websites' : 'Website'; 
     $links = array_map(
      function($url) { 
       $url = htmlspecialchars($url); 
       return sprintf ('<a href="http://%s">%s</a>', $url, $url); 
      }, 
      $website); 
     ?> 
     <strong><?php echo $label; ?>:</strong> <?php echo implode(', ', $links); ?><br /> 
<?php endif ?> 

回答

0

你可以認真類似於你已經嘗試自己,並使其發揮作用的方式虐待implode,但是這真的不是一個好主意。

你需要的是從網址列表移動到錨標籤的列表,這是可能的array_map

if ($website = get_post_meta($post->ID, 'website')) { 
    $links = array_map(
     function($url) { 
      $url = htmlspecialchars($url); 
      return sprintf ('<a href="http://%s">%s</a>', $url, $url); 
     }, 
     $website); 

    echo implode(', ', $links); 
} 
+0

這引發了php錯誤,就好像某些東西沒有關閉一樣。無法弄清楚什麼... –

+0

錯誤是什麼意思? –

+0

我在代碼編輯器中,它不會「說」任何東西,它只是在它旁邊有一個警告顏色,並且它下面的代碼的所有顏色都會改變。我試圖在最後一個花括號中添加一個分號,但它也不喜歡這個。你介意編輯它以包含'<?php'位?也許我只是凌晨託德頭,並沒有正確實施它? –

1
<?php 
if($website = get_post_meta($post->ID, 'website')): 
    $label = count($website) > 1 ? 'Websites' : 'Website'; 
    ?> 
    <strong><?php echo $label; ?>:</strong> 
    <?php foreach($website AS $w): ?> 
     <a href="<?php echo $w; ?>" target="_blank"><?php echo $w; ?></a> <br /> 
    <?php endforeach; ?>  
<?php endif; ?> 

這使得假設你的數組中的每一個「網站」是一個完整的有效網址,其中包括http://

我認爲問題的根源在於理解implode的作用,以及爲什麼它不會按照您的方式工作。

編輯:

我明白了,你希望他們在逗號分隔的內嵌列表。你應該使用喬恩的方法,因爲它會做你想要比我在這裏建議的更優雅。

+0

這將返回值,但不考慮逗號...如果我將一個逗號添加到該函數,它會將其添加到每個網址,包括最後一個網址。你沒有理解內爆是正確的。我是lennening! –

0

您需要使用裝飾器模式。修改下面的例子以適應您的需求

interface HtmlElementInterface { 
    public function getText(); 
    public function render(); 
} 

class LinkDecorator implements HtmlElementInterface { 

    protected $title; 
    protected $text; 

    public function __construct($text, $title, $renderNow = false) { 
     if (!is_string($text) || empty($text)) { 
      throw new InvalidArgumentException("The text of the element must be a non-empty string."); 
     } 
     $this->text = $text; 
     $this->title = $title; 
     if ($renderNow) { 
      echo $this->render(); 
     } 
    } 

    public function getText() { 
     return $this->text; 
    } 

    public function render() { 
     // do your magic here 
     return "<a href='" . $this->text . "'>" . $this->title . "</a>"; 
    } 

}