如何将php变量插入Wordpress短代码?

时间:2014-09-27 13:43:39

标签: php wordpress variables

我在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")]' );?>

但这不起作用,有什么想法吗?

1 个答案:

答案 0 :(得分:2)

这种做法怎么样?

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 ...

相关问题