【问题标题】:How to use wordpress functions in an ajax call如何在 ajax 调用中使用 wordpress 函数
【发布时间】:2012-06-04 03:53:05
【问题描述】:

我想知道是否有办法在 ajax 调用中使用类似 query_post() 的函数?

假设我正在调用文件 _inc/ajax.php

我想能够使用 wordpress 功能,但我不知道为什么。 有人可以帮我吗?

非常感谢:)

【问题讨论】:

    标签: ajax wordpress function jquery


    【解决方案1】:

    WordPress 提供了一个 Ajax Url,您应该将其与完整的 Ajax API 一起使用。

    你需要创建一个 jQuery 函数。

    例子:

    jQuery(document).ready(function($) {
    
        var data = {
            action: 'my_action',
            whatever: 1234
        };
    
        jQuery.post(ajaxurl, data, function(response) {
            alert('Got this from the server: ' + response);
        });
    });
    

    ajaxurl var 在管理端始终可用。如果你在前端使用它,你需要定义它。

    然后是一个 PHP 函数,您可以在其中运行查询。 PHP 函数必须附加到 wp_ajax_your_action 操作。

    例子:

    add_action('wp_ajax_my_action', 'my_action_callback');
    
    function my_action_callback() {
        global $wpdb; // this is how you get access to the database
    
        $whatever = intval( $_POST['whatever'] );
    
        $whatever += 10;
    
            echo $whatever;
    
        die(); // this is required to return a proper result
    }
    

    wp_ajax_your_action 操作适用于管理员,如果您需要在前端使用它,该操作将是 wp_ajax_nopriv_your_action

    【讨论】:

      【解决方案2】:

      我强烈推荐 JSON API 插件:http://wordpress.org/extend/plugins/json-api/。它为最常见的 WordPress 功能(包括 query_post)提供了一个 RESTful 接口,并允许您添加自己的操作。

      【讨论】:

      • 嘿,谢谢,但是否可以使用此 API 检索一些纯文本?还是只是我需要用 javascript 设计的一些数据?
      【解决方案3】:

      你必须在你的主题functions.php中创建你的函数 这是使用 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));
          ?>
          <article id="<?php echo $populardata->post_slug.'_mp';?>" data-attr-post-title="<?php echo $populardata->title; ?>" <?php post_class(); ?>>
            <p class="advt_disclosure"><?php echo advtdescloser(); ?></p>
            <?php 
              echo '<h6>'.getCategoryLink().'</h6>';
              $post_mp = get_post($cur_id);
            ?>
            <h1><?php echo $populardata->title;?></h1>
            <div class="entry-content">
              <div class="post-details"> <?php echo getAuthorData($post_mp->post_author,$cur_id,$populardata->title,$cur_date); ?> </div>
              <div class="collapsediv">
                <div class="row">
                  <div class="col-sm-9 post_article">
                    <?php
                          $content = $populardata->postcontent; //$content_post->post_content;
                          $content = apply_filters('the_content', $content);
                          $content = str_replace(']]>', ']]&gt;', $content);
                          echo $content;
                          wp_link_pages( array(
                              'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',
                              'after'       => '</div>',
                              'link_before' => '<span>',
                              'link_after'  => '</span>',
                              'pagelink'    => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',
                              'separator'   => '<span class="screen-reader-text">, </span>',
                          ) ); 
                      ?>
                    <p class="tags"> TAGS: <?php echo get_the_tag_list( '', __( ', ', 'twentyfifteen' ) )?></p>
                  </div>
                  <div class="col-sm-3 hot_deal"> <?php echo getAdvertisements(); ?> </div>
                  <div class="col-sm-12 comment_section"> <?php echo getPostCommentsandSocialmediasharing($post_mp->post_author,$cur_id,$populardata->title); ?> </div>
                </div>
              </div>
            </div>
            <footer class="entry-footer">
              <?php //twentyfifteen_entry_meta(); ?>
              <?php //edit_post_link( __( 'Edit', 'twentyfifteen' ), '<span class="edit-link">', '</span>' ); ?>
            </footer>
            <div class="expander">
              <button class="expand" data-text-swap="Close article">expand article</button>
            </div>
          </article>
          <?php }
          exit();
      }
      add_action('wp_ajax_getPopularPosts', 'getPopularPosts');
      add_action('wp_ajax_nopriv_getPopularPosts', 'getPopularPosts');`
      
      And for call ajax you need to put some where in your themes footer.php
      
      `$(".popular_posts").click(function(e) {
                          e.preventDefault();
                          if($('.popularposts').html().length==0)
                          {
                              $('.popularposts').html('<p class="text-center" style="margin-top:40px;"><img src="<?php echo get_home_url(); ?>/wp-content/themes/twentyfifteen/images/ajax-loader.gif"></p>');
                              $.ajax({
                                  type: 'POST',
                                  url: "<?php echo get_home_url(); ?>/wp-admin/admin-ajax.php",
                                  data: {
                                      action: 'getPopularPosts'
                                  },
                                  success: function( data ) {
                                      $('.popularposts').html(data);
                                  }
                              });
                          }
                      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-23
        • 2017-09-19
        • 1970-01-01
        • 2021-06-29
        • 1970-01-01
        • 2018-02-14
        • 1970-01-01
        • 2016-04-10
        相关资源
        最近更新 更多