2012-10-12 52 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>"; 
    } 

}