admin管理员组

文章数量:1425016

I have a Wordpress AJAX call that is working in as much as it is able to get raw data from a PHP file. The AJAX is using the admin-ajax.php file. However, the PHP file I need to get is within the loop of the page. Where it is being placed is where it works if it was just included on the page.

To summarize: if the PHP file being AJAXed has in it:

<?php echo 'this is the file!' ?>

…everything works fine. But if it has:

<?php the_title(); ?>

…it returns blank.

Thank you for taking the time to read this!

I have a Wordpress AJAX call that is working in as much as it is able to get raw data from a PHP file. The AJAX is using the admin-ajax.php file. However, the PHP file I need to get is within the loop of the page. Where it is being placed is where it works if it was just included on the page.

To summarize: if the PHP file being AJAXed has in it:

<?php echo 'this is the file!' ?>

…everything works fine. But if it has:

<?php the_title(); ?>

…it returns blank.

Thank you for taking the time to read this!

Share Improve this question edited Jun 19, 2019 at 16:14 jons asked Jun 19, 2019 at 15:46 jonsjons 1291 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

There is no such thing as an AJAX call that's inside the loop. The server has no idea what page or where on that page an ajax call is coming from unless you explicitly send it that data as a part of the ajax call. You're going to need to send some data to the php function so it can recreate the correct query (or the current post if that's all you need.) i.e. You send a post ID in the ajax, the php function it calls then does $post_id = $_POST[post_id]; (please add your own sanitization and validation here.) Then instead of calling the_title() you would call get_the_title($post_id)

本文标签: Wordpress AJAX PHP(html) file that is within Wordpress Loop