在没有插件的情况下使用内置在thickbox中的wordpress

Dav*_*vey 6 html css php wordpress jquery

我试图在我的主题中使用wordpress中的内置厚箱.我试图让它成为所有我通过管理员添加的图片自动具有thickbox功能.我尝试将它放在functions.php中,但它不起作用:

function fb_add_thickbox($content){
$content = preg_replace('/<a(.*?)href="(.*?).(jpg|jpeg|png|gif|bmp|ico)"(.*?)><img/U', '<a$1href="$2.$3" $4 class="thickbox"><img', $content);
return $content;
}
add_filter('the_content', 'fb_add_thickbox', 2);
Run Code Online (Sandbox Code Playgroud)

Bos*_*h99 2

假设您的代码实际上正在工作 - (您的标记实际上被过滤了吗?) - 这将失败,因为厚盒尚未激活。您必须手动注入它:

正如 @Alexcp 所指出的 - 您必须手动注册并排队 javascript 和 css(在管理部分之外)。除了 preg_replace 函数之外,还将以下内容添加到模板的Functions.php.

// register scripts 
if (! function_exists(thickbox_register){
function thickbox_register() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery','http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
    wp_register_script( 'thickbox', 'path to thickbox'.thickbox.js, 'jquery');
    }  
}   
add_action('init', 'thickbox_register');

//print the now registered scripts 
if (! function_exists(thickbox_enqueue){
function thickbox_enqueue() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'thickbox' );
    }  
}
add_action('wp_print_scripts', 'thickbox_enqueue');

// do the same for css 
if (! function_exists(thickbox_style_enqueue){
function thickbox_style_enqueue() {
    wp_register_style( 'thickbox', 'path to css'.thickbox.css );
    wp_enqueue_style( 'thickbox' );
    }  
}
add_action('wp_print_styles', 'thickbox_style_enqueue');
Run Code Online (Sandbox Code Playgroud)

请注意,可以通过多种方式获取路径- 但类似的内容bloginfo('url');应该可以帮助您入门。

如果仍然有问题,请使用 FireBug 或类似的东西来确保在 DOM 的 jquery 对象中正确注册厚盒。

希望有帮助