javascript - 在 Javascript 中使用 Wordpress 定制器

标签 javascript php wordpress

我已经为 Wordpress Customizer 制作了一个自定义控件,我想在脚本 (Instafeed.js) 中设置我的控件,以更改 limit 数字。

正在关注 this answer到目前为止我就是这样做的

<script type="text/javascript">  
          var userFeed = new Instafeed({
            get: '',
            tagName: '',
            clientId: '',
            limit: var imglimit = <?php echo json_encode($imglimit); ?>;,
            });
            userFeed.run();
</script>

函数

$wp_customize->add_setting(
        'imglimit',
        array(
            'default'      => '',
            'section'      => 'section',
));
$wp_customize->add_control('imglimit', array(
      'label'      => __('test'),
      'section'    => 'section',
      'settings'   => 'imglimit',
      'type'       => 'select',
      'choices'    => array(
        '5'   => '5',
        '10'  => '10',
        '20'  => '20',
      ),
));

function theme_customizer()
{
    $imglimit = get_theme_mod('imglimit');
}

谁能告诉我错在哪里?我已经搜索了一段时间。

最佳答案

好吧,你这里有一个语法错误:)

      var userFeed = new Instafeed({
        get: '',
        tagName: '',
        clientId: '',
        limit: var imglimit = <?php echo json_encode($imglimit); ?>;,
//             ^^^^^^^^^^^^ here                      and here     ^ 
        });

因此,您应该将该代码块更改为

      var userFeed = new Instafeed({
        get: '',
        tagName: '',
        clientId: '',
        limit: <?php echo json_encode($imglimit); ?>,
      });

实际上你不一定需要在这里进行json编码,因为它只是一个数字。但如果那是某个数组或对象,是的,您应该对其进行编码。

在你的 php 代码中,你应该将 $imglimit 设为全局:

function theme_customizer()
{
    global $imglimit;
    $imglimit = get_theme_mod('imglimit');
} 

或者只是将其放入 js 中:

      var userFeed = new Instafeed({
        get: '',
        tagName: '',
        clientId: '',
        limit: <?php echo json_encode(get_theme_mod('imglimit')); ?>,
      });

关于javascript - 在 Javascript 中使用 Wordpress 定制器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34471652/

相关文章:

javascript - javascript 中的简单 Prolog 谓词

php - 单击执行按钮后如何保留选定的列表值?

php - vagrant : Failed to mount folders in Linux guest, "vboxsf"文件系统不可用?

php - WooCommerce 可变产品 : keep only "min" price with a custom label

wordpress - WooCommerce - 买 10 件送 3 件 - 只有最便宜的 3 件免费

javascript - HTMLImageElement 节点而不是真实图像

javascript - 修补 FormBuilder 表单中的值并观察更改会导致无限循环

php - WooCommerce - 如果数量超过 1,请单独处理购物车商品

javascript - 为什么函数内部用let accessible描述的变量?

php - Apache 2 + PHP 5 : how to redirect an user to another domain