簡體   English   中英

如何將php變量插入Wordpress短代碼?

[英]How do I insert a php variable into a Wordpress shortcode?

我在Wordpress中設置自定義字段,以便能夠從亞馬遜輸入ASIN號碼並讓它通過我在模板文件中的短代碼。

在我的自定義字段中,我使用Name:asin和Value :(無論我想放入哪個ASIN)

這是我目前在模板文件中的內容:

<?php $asin = get_post_meta( $post->ID, 'asin', true); ?>

<?php echo $asin ?>

<?php echo do_shortcode( '[scrapeazon asin="B002P4SMHM" width="650" height="400" border="false" country="us")]' );?>

我試圖將變量$ asin放入我擁有的scrapeazon短代碼中,如下所示:

<?php echo do_shortcode( '[scrapeazon asin="<?php echo $asin ?" width="650" height="400" border="false" country="us")]' );?>

但這不起作用,任何想法?

這種方法怎么樣?

function my_shortcode_function( $attributes ) {
    global $post;

    // manage attributes
    $a = shortcode_atts( array(
        'foo' => 'something',
        'bar' => 'something else',
    ), $attributes );

    // do sth with the custom field
    $asin = get_post_meta( $post->ID, 'asin', true);
}

add_shortcode('my_shortcode', 'my_shortcode_function');

所以不要試圖在模板中獲取自定義值。 只需在短代碼功能中處理它。 通過全局聲明的$ post變量,它應該工作。 是的,全球不是很干凈。 但它是wordpress ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM