我正在尝试将ShortCode中的值转换为变量,以便在模板文件中使用它。我该怎么做呢?
下面是我的代码:
在帖子中,简短的代码如下:
[reference_prix]1-214eddz[/reference_prix]我的插件代码:
$bl_reference_prix = "";
add_shortcode('reference_prix', 'get_blref_prix');
function get_blref_prix( $atts, $reference = null ) {
global $bl_reference_prix;
$bl_reference_prix = $reference;
}但是$bl_reference_prix仍然是空的。
我尝试过$GLOBAL[],但我也遇到了同样的问题。
获取用户在wordpress帖子中写入的值并在模板文件中显示(或使用它)的最佳实践是什么?
发布于 2014-04-30 12:36:59
我认为最佳实践是使用atts参数。
// Add Shortcode
function get_blref_prix( $atts ) {
// Attributes
extract( shortcode_atts(
array(
'bl_reference_prix' => '',
), $atts )
);
}
add_shortcode( 'reference_prix', 'get_blref_prix' );使用短码的用户只需在编辑器中执行以下操作:
[reference_prix bl_reference_prix="some value by the user"]然后也许你可以尝试使用Options API。使用后添加和删除。
发布于 2014-05-01 05:27:35
我已经这样做了,它现在的工作方式是:
//Plugin
function get_blref_prix( $atts ) {
global $bl_plugin_refprix, $bl_plugin_refprix_up;
// Attributes
extract( shortcode_atts(
array(
'reference' => '',
'up' => '',
), $atts )
);
$bl_plugin_refprix = $reference;
$bl_plugin_refprix_up = $up;
}
add_shortcode( 'bl_refprix', 'get_blref_prix' );在模板文件中(重要:在函数“the_content”之后!):
while(have_posts()):the_post();
echo the_content();
endwhile;
echo $bl_plugin_refprix;在《华盛顿邮报》:
[bl_refprix reference="123" up="456"]https://stackoverflow.com/questions/23376775
复制相似问题