I am trying to implement fancybox into WP without using a plugin. I did some research and went over few examples but there is still an issue. Any help would be greatly appreciated.
Here’s the code:
in functions.php:
function loadFancyBox()
{
if (is_single() && strpos($post->post_content,'class="fancy"') !== false)
{
wp_enqueue_style('fancyStyle', get_template_directory_uri() . 'fancyBox/jquery.fancybox.css');
wp_enqueue_script('fancyScript', get_template_directory_uri() . 'fancyBox/jquery.fancybox-1.3.4.js', array('jquery'), '', true );
}
}
add_action('wp_print_styles', 'loadFancyBox');
and in my header.php first I call jQuery and right after:
<script>
$(document).ready(function() {
$("a.fancy").fancybox({
'transitionIn' : 'elastic'
});
});
</script>
and finally I add class=”fancy” to any img a tag. At this point any link goes to the image but fancybox is not being loaded.
Thank you
If you check your console for errors as @patnz suggested, you’ll likely see that $ is undefined. This is because WordPress loads jQuery in noConflict mode. Try this instead:
Cool, so now you know the js is fine. Next you want to check if your conditional statement is working correctly. is_single() only returns true on a post page. is_singular() will return true on posts, pages, attachments. You may need to globalise $post inside the function. Adding a few echos into the function will just help you see where it’s breaking.
For testing purposes I would add “|| 1==1 ” to your conditional test to make it always true.
Then you can make sure the paths to your scripts are being printed correctly by viewing the source code. Find them in the head and click on them which will either show you the script source or the source of a 404 page. If you get the 404 you’ll need to fix location parameter in you enqueue functions.
Also I would hook that function onto wp_head instead of wp_print_styles since you’re adding a script also. So try the following as a test. Since you now know the js is working it’s really just a matter of narrowing down the possibilities as to why the php isn’t working.