2014-01-10 2 views
1

WordPress 내에서 작동하는 AJAX 기능이 있습니다. HTML을 통해 내 AJAX Success CB에 액세스 할 수있는 내 게시물을 얻으려고 반복합니다. 여기에서 div에 추가됩니다.jSON AJAX 요청에 대한 PHP 쿼리 서식 지정 방법

내 질문 :

어떻게 내 PHP 쿼리 FUNC 값 자바 스크립트 내에서 적절한 사용 (while 루프, 배열 및 여러 별도의 페이지 매김 값) 포맷 것인가?

내 기능은 다음과 같습니다.

<?php 
function _custom_paginate(){ 

    $paged = $_POST['count']; 
    $args = array(
     'posts_per_page' => 12, 
     'category'  => 3, 
     'paged'   => $paged, 
     'post_status'  => 'publish' 
    ); 

    $query = new WP_Query($args); 
    if ($query->have_posts()) : 
    while ($query->have_posts()) : $query->the_post(); ?> 

     <div class="large-3 three columns box"> 
     <div class="thumb"> 
      <?php the_content();?> 
      <div class="thumb-foot"> 
      <div class="thumbinfo"> 
       <p>Posted on <?php the_time('m.d.Y') ?></p> 
      </div> 
      <div class="thumb-social">    
       <div class="thumb-twitter"></div> 
       <div class="thumb-facebook"></div> 
      </div> 
      </div> 
     </div> 
     </div> 
    <?php 
    endwhile; 
    endif; 

    //Several pagination values 
    $total_pages = $query->max_num_pages; 
    if ($total_pages > 1){ 
    $current_page = max(1, get_query_var('paged')); 
    ?> 
    <div class="pagi-contain"> 
     <div id="pagi" class="large-12"> 

     <!-- Append pagination here #pagi--> 

     <ul class="pagi"> 
      <li id="ajaxprev" style="float:left;">prev</li> 
      <li id="pagenum"><?php echo $current_page; echo ' ... '.$total_pages; ?></li> 
      <li id="total_pages"></li> 
      <li id="ajaxnext" style="float:right;">next</li> 
     </ul> 

     </div> 
    </div> 
    <?php 
} 
exit; 

} 당신은 JS 별도로 각각의 값을 사용하기 위해서는 접근 배열 (?)에 각 값을 포맷이 아니라 교훈을 다시 작성할 필요가 없습니다

. 나는 this person이 내가 시도한 것을 수행 한 것으로 보이지만 여기에 주제가 PHP 쪽이 아님을 알았습니다 ...

다음은 제가 작업을 시도하고있는 것에 대해 더 명확한 예입니다. http://jsfiddle.net/BenRacicot/LRmc3/

+0

http://us1.php.net/json_encode – bobkingof12vs

+0

예 json_encode 배열에 대한 함수를 알고 있습니다. 문제는 '어떻게 쿼리 컨텐츠 html과 다른 값을 하나의 배열에 저장합니까?'입니다. – BenRacicot

답변

2

난 당신이 포맷 할 데이터를 100 % 분명 아니지만, 그냥이 수행

json_encode($variable_you_want_as_json); 

전체 페이지의 출력 형식으로를, 이것을 사용 :

ob_start(); // Start output buffering (echos after this go into the buffer, not to the client) 

// all the code and output you want formatted 
// example: echo "Some HTML I want to store in a JSON-encoded variable"; 

$html_data = ob_get_contents(); // Gets the contents of the buffer 
ob_end_clean(); // empties the buffer 
echo json_encode($html_data); // encodes and echoes whatever you did between ob_start() and ob_get_contents() 

는 소리가 난다 여러 값을 원했기 때문에 다음과 같이 마지막 행의 변수를 배열로 바꿀 수 있습니다.

$my_array = array('html_data'=>$html_data, 
    'max_num_pages' => $query->max_num_pages, 
    'foo'=>$foo, 
    'bar'=>$bar, 
    'some_other_var' => $some_other_var); 
echo json_encode($my_array); 
+0

그건 내 질문이 아니야 – BenRacicot

+0

@BenRacicot 질문이 명확하지 않습니다. 어떻게 데이터를 저장 하시겠습니까? "쿼리 컨텐츠 html과 다른 값을 하나의 배열에 저장하려면 어떻게해야합니까?" 분명한 질문이 아닙니다. 어떤 가치가 있습니까? –

+1

@BenRacicot 귀하의 질문은 매우 명확하지 않습니다, IE는 '나는 당신이 포맷하고 싶은 데이터가 100 % 명확하지 않습니다.'라고 말합니다. 그러나 배열과 마찬가지로 json_encode에 값을 제공 할 수 있습니다. ie json_encode (array ('html'=> $ html, 'result'=> $ result)); 등등 – Rottingham