How to use wordpress functions in an ajax call

前端 未结 3 1426
既然无缘
既然无缘 2020-12-24 09:02

I wanted to know if there is a way to use function like query_post() in an ajax call ?

Let\'s say i\'m calling the file _inc/ajax.php

I want to be abble to

3条回答
  •  猫巷女王i
    2020-12-24 09:39

    You have to create you function in your themes functions.php Here is the example for load popular posts using ajax

    function getPopularPosts()
    {
        $popularpostdata = wpp_get_mostpopular_data();
        foreach($popularpostdata as $populardata)
            {
                $cur_id = $populardata->id;
                $cur_date = $populardata->post_date;
                $cur_date = date("F j, Y", strtotime($cur_date));
        ?>
        
    >

    '.getCategoryLink().''; $post_mp = get_post($cur_id); ?>

    title;?>

    post_author,$cur_id,$populardata->title,$cur_date); ?>
    postcontent; //$content_post->post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content; wp_link_pages( array( 'before' => '', 'link_before' => '', 'link_after' => '', 'pagelink' => '' . __( 'Page', 'twentyfifteen' ) . ' %', 'separator' => ', ', ) ); ?>

    TAGS:

    post_author,$cur_id,$populardata->title); ?>
    ', '' ); ?>

    '); $.ajax({ type: 'POST', url: "/wp-admin/admin-ajax.php", data: { action: 'getPopularPosts' }, success: function( data ) { $('.popularposts').html(data); } }); } });

提交回复
热议问题