2014-02-11 28 views
0

我對編碼很陌生,所以對我這麼簡單。我將我的一些html移動到我的functions.php文件中,並將這些html更改爲一個函數,然後我可以根據需要將它們回調到我的模板文件中。在一個函數中改變html

這是我的HTML與URL的作者框,將連接到作者的Facebook和Twitter頁面顯示Twitter和Facebook按鈕的只是其中的一部分

<div class="profile-links"> 

       <?php if (get_the_author_meta('facebook') != '') ?> 
        <a href="http://www.facebook.com/<?php echo wp_kses(get_the_author_meta('facebook'), null); ?>" title="<?php printf(esc_attr__('Follow %s on Facebook', 'pietergoosen'), get_the_author()); ?>"> 
         <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/facebook.png" height="32px" width="32px" alt="<?php printf(esc_attr__('Follow %s on Facebook', 'pietergoosen'), get_the_author()); ?>" /></a> 


       <?php if (get_the_author_meta('twitter') != '') ?> 
        <a href="http://www.twitter.com/<?php echo wp_kses(get_the_author_meta('twitter'), null); ?>" title="<?php printf(esc_attr__('Follow %s on Twitter', 'pietergoosen'), get_the_author()); ?>"> 
         <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/twitter.png" height="32px" width="32px" alt="<?php printf(esc_attr__('Follow %s on twitter', 'pietergoosen'), get_the_author()); ?>" /></a> 


     </div> 

這是到目前爲止的代碼本特定的部分,我把功能

foreach ($services as $name => $service){ 

$authorDetails = get_the_author(); 
$AuthorMeta = get_the_author_meta($service['name']); 

$services = array (
     'facebook' => array (
      'url' => "http://www.facebook.com/' . echo . 'wp_kses($AuthorMeta, null)'", 
      'text' => 'Follow %s on Facebook' 
     ), 
'twitter' => array (
      'url' => "http://www.twitter.com/' . echo . 'wp_kses($AuthorMeta, null)'", 
      'text' => 'Follow %s on Twitter' 
     ) 
    ); 
} 

    $img_base = get_stylesheet_directory_uri() . '/images/%s.png'; 

    foreach ($services as $name => $service){ 
    if ($AuthorMeta != '') { 
     $href = sprintf($service['url']); 
     $src = sprintf($img_base, $name); 
     $size = '32'; 

    echo '<div class="profile-links">'; 

      printf(
       '<a href="%1$s" title="%2$s"><img src="%3$s" alt="%2$s" height="%4$d" width="%4$d"/></a>', 
       $href, 
       esc_attr(__($service['text'], 'pietergoosen'), $authorDetails), 
       $src, 
      $size 
     ); 

    echo '</div>'; 

    } 
} 

我的問題是,我沒有得到正確的URL。如何將html中的URL轉換爲我必須在我的函數的'url'字段中使用的URL。我希望我提供的所有代碼都有意義

+0

你應該爲wordpress標記這個 – Namal

+0

這是一個WordPress的問題嗎? – Hamish

+0

請參閱:http://wordpress.stackexchange.com/ – Hamish

回答

0

連接值時不需要編寫echo。並且對函數的調用不應放在引號內。所以它應該是:

$services = array (
    'facebook' => array (
     'url' => "http://www.facebook.com/" . wp_kses($AuthorMeta, null), 
     'text' => 'Follow %s on Facebook' 
    ), 
    'twitter' => array (
     'url' => "http://www.twitter.com/" . wp_kses($AuthorMeta, null), 
     'text' => 'Follow %s on Twitter' 
    ) 
); 
+0

謝謝巴爾瑪。 –

相關問題