0

I'm trying to create a lightbox effect for a self-made WordPress Theme. But I'm trying to include a WordPress page in the body through jQuery.

For example, in my js file.

    $('.button').Click(function(){

    $(body).append('`<div><?php get_template_part('content','thing'); ?></div>`');

});

I've tryed that and then my php file doesn't reproduce the php bit.

Thanks.

RoLA
  • 69
  • 1
  • 2
  • 2
    PHP won't be parsed in `*.js` files by default. Either rename your JavaScript file to end in `.php` or edit your `.htaccess`, [Clicky](http://stackoverflow.com/a/3943261). – Dave Chen Oct 05 '15 at 19:04
  • [this article](http://premium.wpmudev.org/blog/using-ajax-with-wordpress/) on how to use ajax in wordpress may help you. and [these](https://codex.wordpress.org/AJAX_in_Plugins#Further_Reading_-_External_Resources) are other resources on the subject – d79 Oct 05 '15 at 19:51

2 Answers2

1

try

  $('.button').Click(function(){

    $(body).append("<div><?php get_template_part('content','thing'); ?></div>");

});

Remember to use this jquery in a php file not in JS file.


it might help you

urfusion
  • 5,528
  • 5
  • 50
  • 87
1

You can call a ajax functions that returns your php result.

Like (Seems you're using jQuery):

$.get("templatepart.php", function(data) {
  $(body).append("<div>"+ data +"</div>");
})

And in your php file ("templatepart.php"):

<?php
  // after includes/requires/etc functions
  get_template_part('content','thing');
?>
  • 1
    this wouldn't work because wordpress is not loaded in templatepart.php, so `get_template_part` is not defined – d79 Oct 05 '15 at 19:48