If this is in a widget class, you need…
add_action('wp_ajax_my_func', array($this,'my_func'));
… not just…
add_action('wp_ajax_my_func', 'my_func');
However, I suspect there to be more problems than just that due to the context you are using this in. But without more code, that is all I’ve got.
Based on new information in the question…
Both your function definitions and the add_action
code is nested inside the widget
method of your Widget. That method only runs when the widget displays on the front end, so nothing inside it is going to execute for a AJAX request. You need to pull that code out of the widget
method and then hook it in the Widget’s constructor, something like this:
public function __construct() {
// other constructor stuff
add_action('wp_ajax_loadMoreP', array($this,'loadMoreP'));
add_action('wp_ajax_nopriv_loadMoreP', array($this,'loadMoreP'));
}
//ajax func.
public function loadMoreP($number){
query_posts('showposts=".$number."');
while(have_posts()) : the_post();
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a>'.the_post_thumbnail('thumbnail').'</li><br>';
endwhile;
wp_reset_query();
die();
}
That is pseudo-code really. It is completely untested and probably won’t work without being adapted to your particular widget.