2013-12-17 60 views
5

在我的WordPress博文中,我創建了一個未知數的類型爲my_player的簡碼,並正確添加了鉤子。我想知道是否有某種類型的WordPress功能可以將您的內容傳遞給簡碼名稱,並且它可以爲您提供一組匹配的簡碼,其屬性索引爲屬性名稱。類似下面我的代碼...如何從帖子內容中提取WordPress短代碼屬性

$matches = get_shortcode_matches($content, "my_player"); 

for($i = 0; $i < sizeof($matches); $i++) 
{ 
    $title = $matches[$i]['title']; 
    $mp3 = $matches[$i]['mp3']; 
    $soundcloud = $matches[$i]['soundcloud']; 
} 

我知道,當你創建使用add_shortcode()功能就像我上面你可以使用這些索引值的簡碼鉤,但我需要有一個功能,可以稍後和循環之外訪問它們。 WordPress有沒有這樣的功能?

回答

0

試試這個,

<?php do_shortcode($content); ?> 
+2

我已經使用了這些。這雖然沒有幫助提取任何屬性及其值。 – jas7457

2

有這樣做的幾種方法:1。 編寫自己的代碼片段如同上面一樣,插入您的「μ插件」以下文件夾

// [myplayer attr="your-value"] 
function myplayer_func($atts) { 
    extract(shortcode_atts(array(
     'title' => 'something', 
     'mp3' => 'another something', 
       'soundcloud' => 'something else', 
    ), $atts)); 

    return "attr= {$attr}"; 
} 
add_shortcode('myplayer', 'myplayer_func'); 

然後

[myplayer title="something" mp3="another something" soundcloud="something else"] 
從任何地方,包括子域的任何職位

。 2.你可以使用像ShortcoderGlobal Content Blocks

+0

我說我已經設置了鉤子和'add_shortcode()'函數。我知道你可以讓它使用這個函數中的這些信息,但我想要一個函數給出簡碼名稱,搜索內容,並返回一個名稱 - 值對的數組。類似於我的原始文章中編寫的代碼。 – jas7457

+0

在WordPress編碼標準中不鼓勵使用糟糕的extract()函數。就這樣你知道。 – redanimalwar

0

插件我不認爲你可以通過使用現有的行動。 [shortcode_parse_atts]沒有附加任何事件。 你可以做到這一點的唯一方法就是add_action/global在每個[add_shortcode]相關函數/方法中都是有價值的。

喜歡的東西:

function doShortCode($atts, $content=null){ 
    global $my_shortcode_storage; 
    $my_shortcode_storage['my_shortcode_1'][] = $atts; 
}