Skip to content
Advertisement

AJAX function in the widget class

I created a WordPress Widget which get the recent posts, first get a specific number of posts, then there is a button to get more posts by AJAX.

The full Widget code

JavaScript

And this is the AJAX code to get ajaxloadMore function output and append to the ul tag

JavaScript

How to get $catid and $number variables from the function widget and put them in ajaxloadMore function?

How to filter query_posts in ajaxloadMore function like the function widget by the same function filter_where?

Advertisement

Answer

Both questions have the same answer, use the same method as in data-offset:

JavaScript

then

JavaScript

and finally use $_POST['catid'] and $_POST['where'] in the ajaxloadMore() function.

I built the $ajax_url with $ajax_url = admin_url('admin-ajax.php');.

But, nonetheless, try to use AJAX in WordPress like this and don’t use query_posts.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement