2016-03-04 38 views
2

我在我的網站上有一個按鈕簡碼,我將在所有帖子中使用它。 我想要的是,如果我修改了按鈕值,更改應該應用到網站上的所有按鈕。所以我不必修改每一個帖子。簡碼作爲wordpress中的一個值

我想更改按鈕標題鏈接,也許相對

例如:

[button rel="[btn-rel]" url="[btn-url]"] [btn-cap] [/button] 

我一派的問題,並添加以下代碼Function.php更改按鈕標題。

function btn_cap() { 
    return 'More Freebies!'; 
} 
add_shortcode('btn-cap', 'btn_cap'); 

,並添加簡碼的按鈕簡碼是這樣的:

[button][btn-cap][/button] 

哪個沒有制訂出:(按鈕的標題爲「[BTN-帽]」而不是「更多贈品「

我真的希望這件事工作,因爲這將節省我很多的時間和工作。提前

謝謝...

+1

爲什麼不像[button rel =「the-rel」url =「the-url」] [/ button]那樣向短代碼添加參數,然後在短代碼中處理這些參數?有關簡碼參數的更多信息,請訪問https://codex.wordpress.org/Shortcode_API –

+0

截至目前,我只想更改**按鈕標題**。因爲我可以在需要時重定向鏈接。但問題是與按鈕標題..我想控制從後端按鈕的標題。 PHP對我來說是新的,所以沒有一個想法。它也可以使用Javascript來完成?我沒有關於如何在簡碼中使用簡碼的想法。 – Piyush

+0

你可以設置一個標題屬性,如[button title =「button-title」] [/ button],然後在你的functions.php中進行處理。說實話,答案已經在下面解釋得很清楚了,應該能夠在這裏幫助你 –

回答

3

您不能像示例中那樣使用簡碼作爲變量/佔位符。但是,您可以使用所需的默認值編寫自己的按鈕簡碼。

以下添加到的functions.php

function freebie_button($atts, $content = null) { 
    // Default values 
    $atts = shortcode_atts(array(
      'rel' => 'nofollow', 
      'url' => '/', 
      'cap' => 'More Freebies!', 
     ), $atts); 

    $rel = 'rel="' . $atts['rel'] . '" '; 
    // remove 'rel' attribute if it's empty 
    if($rel === ''){ 
     $rel = ''; 
    } 

    return '<a ' . $rel . 'href="' . $atts['url'] . '">' . $atts['cap'] . do_shortcode($content) . '</a>'; 
} 
add_shortcode('freebutton', 'freebie_button'); 

用途

[freebutton] 
[freebutton rel="" url="#" cap="Buy this"] 
[freebutton cap="Read "]More[/freebutton] 

輸出

<a rel="nofollow" href="/">More Freebies!</a> 
<a href="#">Buy this</a> 
<a rel="nofollow" href="/">Read More</a> 

充滿希望這個代碼會讓你開始。

+0

如果保存了function.php文件,代碼將返回500服務器錯誤! – Piyush

+0

問題出在返回行 – Piyush

+0

@Krypto是正確的。它應該是'return'