gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:08:57 24 4
gpt4 key购买 nike

我正在 Wordpress 中设置自定义字段,以便能够输入来自亚马逊的 ASIN 编号并让它通过我在模板文件中的简码。

在我的自定义字段中,我使用的是名称:asin 和值:(无论我想输入什么 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 变量,它应该可以工作。是的,global ist 不是很干净。但它是 wordpress ...

关于php - 如何将 php 变量插入 Wordpress 短代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26075279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com