使用theme mod值作为默认短代码attr

时间:2018-03-26 05:04:14

标签: php wordpress plugins shortcode

所以我有一个简单的地图短代码,我使用了一个主题mod我已经添加了其他东西作为地址(这显然不是整个事情,但你得到的图片):

<iframe width="100%" height="500" id="gmap_canvas" 
src="https://maps.google.com/maps?q=<?php echo get_theme_mod( 
'primary_location'); ?>&t=&z=13&ie=UTF8&iwloc=&output=embed" frameborder="0" 
scrolling="no" marginheight="0" marginwidth="0"></iframe>

然后我认为应该包括设置高度和宽度的选项,包括默认值。我也认为可以选择使用不同的位置会很好,所以现在它开始于:

function shortcode_map($atts, $content = null) {
extract(shortcode_atts(array(
'height' => '500px', 'width' => '500px', 'location' => ''
), $atts));

ob_start(); ?>

<iframe width="<?php echo esc_attr($width); ?>" height="<?php echo 
esc_attr($height); ?>" id="gmap_canvas" src="https://maps.google.com/maps?q= 
<?php echo esc_attr($location); ?>&t=&z=13&ie=UTF8&iwloc=&output=embed" 
frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>

但我希望默认位置仍然是主要位置,并且真的不知道如何开始排序。

1 个答案:

答案 0 :(得分:0)

这最终起作用了:

function shortcode_map($atts, $content = null) {

$location = get_theme_mod('primary_location');
extract(shortcode_atts(array('height' => '500px', 'width' => '500px', 
'location' => $location), $atts));
 ob_start(); ?>

<iframe width="<?php echo esc_attr($width); ?>" height="<?php echo 
esc_attr($height); ?>" id="gmap_canvas" src="https://maps.google.com/maps?q= 
<?php echo esc_attr($location); ?>&t=&z=13&ie=UTF8&iwloc=&output=embed" 
frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>