2016-09-21 30 views
1

我有來自不同開發人員的多個插件,這些插件允許我顯示亞馬遜產品。 他們都要求我在他們的短碼內輸入亞馬遜類別ID。WordPress將變量傳遞給另一個簡碼

[amazon bestseller="1234567"] 
[amz_links node="1234567"] 
[amazon_toprated category="1234567"] 

我現在用的這些簡碼,在巨大的頁面數量,小部件等 每一個,然後我需要改變的ID,它是一個巨大的麻煩手動改變他們。

我的想法是創建自己的短代碼,如[myamazon_id]和進入其他簡碼內部的簡碼:[amazon bestseller="[myamazon_id]"]

可惜,這似乎並沒有工作,因爲這些都是自我封閉的簡碼,其中apperantly不要」允許短代碼內的短代碼。

我現在的解決方案是使用一個插件,它允許我在WordPress頁面和小部件中使用PHP。

[insert_php]echo do_shortcode('[amazon bestseller="'.myamazonid().'"]');[/insert_php]

這並不工作,但我不知道是否有一個更好的解決方案,一個varible傳遞到簡碼。直接在頁面和小部件上使用PHP實際上並不是我感覺良好的。

我的目標將是有這樣的事情:此[amazon bestseller="[myamazon_id]"]

回答

0

一個簡單的解決方案可以插件Post Snippets。使用此插件,您可以創建3個新的簡碼,其中包含3個帶類別ID的亞馬遜簡碼。現在您可以使用新創建的簡碼了,如果您想更改類別ID,則只需在一處編輯即可。

0

與這樣的簡碼[amazon bestseller="myamazon_id"] possbile用以下代碼。

您不需要包裝其他簡碼與這些括號"[""]"

function first_shortcode_amazon($atts) { 
    $newvar = do_shortcode('['.$atts['bestseller'].']'); 
    return $newvar; 
} 
add_shortcode('amazon', 'first_shortcode_amazon'); 


function second_shortcode_for_amazonid($atts) { 
    global $post; 
    // here i have placed global post id. you can do code to get amazon id here 
    return 'AMAZON_ID=> '.$post->ID; 
} 
add_shortcode('myamazon_id', 'second_shortcode_for_amazonid'); 
+0

這看起來像一個很好的解決方案,但我不能得到它的工作。短代碼的行爲就像沒有提供ID一樣,這導致我認爲'''first_shortcode_amazon'''函數不會將'''myamazon_id''解析爲簡碼,而是作爲普通文本解析。 –