javascript - function.php 内的 JQuery 脚本不工作

标签 javascript jquery wordpress wordpress-theming

我无法让这个 Jquery 脚本在我的 function.php wordpress 文件中工作以实现自定义主题。有人能帮我吗。问题是,当我在 WordPress 管理中转到自定义帖子类型时,当我单击浏览按钮插入图像时,它不允许我,它什么也不做。

这是代码(是的,有一个开放的 php 标签,只是没有显示。)

function show_your_images_meta_box() {
global $post;  
    $meta = get_post_meta( $post->ID, 'your_images', true ); ?>

    <input type="hidden" name="your_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">

    <p>
        <label for="your_images[image]">Upload Poster Image</label>
        <br>
        <input type="text" name="your_images[image]" id="your_images[image]" class="meta-image regular-text" value="<?php echo $meta['image']; ?>">
        <input type="button" class="button image-upload" value="Browse">
    </p>
    <div class="image-preview"><img src="<?php echo $meta['image']; ?>" style="max-width: 250px;"></div>


    <script> jQuery(document).ready(function($) {
            // Instantiates the variable that holds the media library frame.
            var meta_image_frame;
            // Runs when the image button is clicked.
            $('.image-upload').click(function(e) {
                // Prevents the default action from occuring.
                e.preventDefault();
                var meta_image = $(this).parent().children('.meta-image');
                // If the frame already exists, re-open it.
                if (meta_image_frame) {
                    meta_image_frame.open();
                    return;
                }
                // Sets up the media library frame
                meta_image_frame = wp.media.frames.meta_image_frame = wp.media({
                    title: meta_image.title,
                    button: {
                        text: meta_image.button
                    }
                });
                // Runs when an image is selected.
                meta_image_frame.on('select', function() {
                    // Grabs the attachment selection and creates a JSON representation of the model.
                    var media_attachment = meta_image_frame.state().get('selection').first().toJSON();
                    // Sends the attachment URL to our custom image input field.
                    meta_image.val(media_attachment.url);
                    //var image_url = meta_image.val();
                    //$(selected).closest('div').find('.image-preview').children('img').attr('src', image_url);
                });
                // Opens the media library frame.
                meta_image_frame.open();
            });
        });

    </script>

    <?php }

最佳答案

可能是 WordPress 附带的 jQuery 默认设置为兼容模式,这意味着 $ 快捷方式不起作用。参见这里:Using ‘$’ instead of ‘jQuery’ in WordPress .

关于javascript - function.php 内的 JQuery 脚本不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39907042/

相关文章:

javascript - 如果 0 为 null,则更改数组中每个对象的值

javascript - 为什么在计划开始新的一个月后月份开始增加?

javascript - Wordpress TinyMCE 向弹出表单添加描述

php - CSS 未链接到 HTML 页面模板

jquery - 找不到 CSS 来使我的 Wordpress 灯箱居中

javascript - 防止文档事件监听器触发?

javascript - 如何使用 Ant Design Vue 在 Select 中禁用特定元素?

javascript - 使用 jquery 从插入的 html 中访问 dom 元素

javascript - 当其中一个变大时,如何使组成网格的 div 保持原位?

jquery - 如何以相反的顺序从多个元素中删除类?