javascript - Wordpress - 针对个人 POST,而不是 POST 类型。 AJAX/PHP

标签 javascript php ajax wordpress

我试图在单击帖子标题时加载单个帖子信息。我已经设法让我的脚本循环遍历帖子数组中的标题,然后单击将帖子标题再次加载到 div 中。我希望它能在点击时加载单个帖子信息。

PHP 数组后函数 -

add_action( "wp_ajax_success_stories", "sa_get_paginated_posts" );
add_action( "wp_ajax_nopriv_success_stories", "sa_get_paginated_posts" );

function sa_get_paginated_posts(){

      $args = array(
          'posts_per_page'  => -1,
          'post_type'       => 'success-stories',
          'orderby'         => 'post_date',
          'order'           => 'DESC' 
      );

      $the_query = new WP_Query( $args );
      $queryitems = $the_query->get_posts();
      $alldata = array();

      if ($queryitems){

        foreach ($queryitems as $query_item){

          $success_story = array();

          $success_story["ID"] = $query_item->ID;
          $success_story["title"] = $query_item->post_title;

          $alldata[] = $success_story;

        }

      }

      $encoded_data = json_encode($alldata);

      echo $encoded_data;

      die();

  }



function sa_get_posts($post_amount, $post_type){

    $args = array(
        'posts_per_page'  => $post_amount,
        'post_type'       => $post_type,
        'offset'          => 0,
        'orderby'         => 'post_date',
        'order'           => 'ASC'

    );

使用此函数循环 -

      <? if ($success_stories): //success story posts ?>
            <ul class="small-block-grid-3">
                <? foreach ($success_stories as $success_story): 
                    $title = $success_story->post_title; ?>

                    <li><a data-id="<? echo $success_story->ID; ?>" class="success-extra" href="#" title=""><? echo $title; ?></a></li>

                <? endforeach; ?>
            </ul>

          <div id="success-list"></div>
          <a class="success-close" href="#" title="">CLOSE</a>

    <? endif; ?>

Javascript/AJAX 调用

  var $json_response_box = $("#success-list");

  function get_ajax_success_posts() {

    //this line gets the id attribute from the link
    var current_story = $(this).attr("data-id");
    console.log(current_story);

    jQuery.ajax({
        type: "GET",
        url: "WORDPRESSDIRECTORY/admin-ajax.php",
        cache: true,
        data: {
            action: "success_stories"
        },
        dataType: "JSON",
        success: function(data) {

            var arrayLength = data.length;

            for (var i = 0; i < arrayLength; i++) {


                var success_id = data[i].ID;
                var success_title = data[i].title;

                console.log(success_id);

                story = '<ul>';
                  story += '<li>';
                    story += success_title;
                  story += '</li>';
                story += '</ul>'; 

                $json_response_box.append(story).fadeIn(1000);

            }
        },
        error: function(errorThrown) {
            console.log("fail-posts");
        }
    });
  }

感谢您的建议,

D

最佳答案

如果我正确理解你的问题,你应该添加另一个 php 函数和 AJAX 调用,以获取点击后的帖子,类似这样的东西(我还添加了随机数来添加一些安全性,将其添加到总是一个好主意你的ajax调用)

HTML:

<li><a data-id="<? echo $success_story->ID; ?>" data-nonce="<?php wp_create_nonce('get_post'.$success_story->ID); ?>" class="success-extra" href="#" title=""><? echo $title; ?></a></li>

PHP:

add_action( "wp_ajax_sa_get_post", "sa_get_post" );
add_action( "wp_ajax_nopriv_sa_get_post", "sa_get_post" );

function sa_get_post(){
    if ( ! wp_verify_nonce( $nonce, 'get_post_'.$POST['id'] ) ) {
    // This nonce is not valid.
    $response['status'] = 'fail'; 
    } else {
    // The nonce was valid.
    $post = get_post($POST['id']);
    $response['status'] = 'success'; 
    $response['post'] = $post;        
    } 
    echo json_encode($response);
    exit(); // this is needed, as otherwise the AJAX returns an extra 0       
}

JS:

 $('.success-extra').on('click', function () {
     jQuery.ajax({
        type: "POST",
        url: "WORDPRESSDIRECTORY/admin-ajax.php",
        cache: false,
        data: {
            action: "sa_get_post",
            id: $(this).attr('data-id'),
            nonce: $(this).attr('data-nonce')
        },
        success: function(data) {
            var result = $.parseJSON(data);
            if (result.status != 'success') {
               // display error messago or something
            } else {

              // do whatever you want with your result.post
            }
        }

 })

希望这有帮助。

关于javascript - Wordpress - 针对个人 POST,而不是 POST 类型。 AJAX/PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25427933/

相关文章:

javascript - 如何使用 knockout 绑定(bind)新元素?

php - 将 url 变量传递到下一页?

php - 使用表单、PHP 和 MYSQL 提交、更新和删除

javascript - 防止在 jQuery 表单上提交

javascript - 将ajax调用中返回的数据与JS函数中的数组值等同

javascript - 动画滚动在 Firefox 中不起作用?

javascript - 带有句法着色的 Json Pretty Print

php - 将Mysql结果输出到数组中

c# - 文本区域内的有序列表(行号)

javascript - 一旦 dom-repeat 的渲染完成就运行一个方法