2015-01-25 4 views
0

이 함수에서 단일 템플릿을 호출하려고하는데 응답이 없습니다. 내가 어디로 잘못 갔니?함수에서 단일 템플릿을 호출 하시겠습니까?

function my_load_ajax_content() { 

    $args = array(
     'p' => $_POST['post_id'], 
     'post_type' => 'projects' 
     ); 

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

    <?php get_single_template() ?> 

    <?php 
    endwhile; 
    wp_die(); 
} 

add_action ('wp_ajax_nopriv_load-content', 'my_load_ajax_content'); // when the user is logged in 
add_action ('wp_ajax_load-content', 'my_load_ajax_content'); // when the user is not logged in 

편집 :이 기능을 사용하기 위해 업로드하고 싶습니다. 그러나 위의 코드는 작동하지 않습니다. 기본적으로 함수의 모든 코드를 동일한 코드를 가진 단일 템플릿 호출로 대체하려고합니다.

Ajax 호출

$.ajax({ 
    type: 'POST', 
    url: ajaxURL, 
    context: this, 
    data: {'action': 'load-content', post_id: post_id }, 
    success: function(response) { 
     $('#project-container').html(response); 
     return false; 
    } 
}); 

이전 기능

function my_load_ajax_content() { 

    $args = array(
     'p' => $_POST['post_id'], 
     'post_type' => 'projects' 
     ); 

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

    <div class="post-container"> 
     <div id="project-left-content"> 
      <?php the_title('<h1 class="entry-title">', '</h1>'); ?> 
      <?php the_content(); ?> 

      <!-- If there is a URL --> 
      <?php if(get_field('url')): ?> 
       <div id="icon-full-wrap"> 
        <div id="icon-link-wrap"> 
         <svg xmlns="http://www.w3.org/2000/svg" id="icon-link" viewBox="0 0 200 200" style="enable-background:new 0 0 200 200;" xml:space="preserve"> 
          <g> 
           <path d="M85.868 85.869c1.599-1.6 3.732-2.48 6.005-2.48c2.273 0 4.4 0.9 6 2.48l16.253 16.3 c0.563 0.6 1.3 0.9 2.1 0.879s1.559-0.316 2.121-0.879l16.253-16.253c0.563-0.562 0.879-1.326 0.879-2.121 s-0.316-1.559-0.879-2.121l-16.253-16.253c-7.081-7.079-16.493-10.977-26.501-10.977c-10.008 0-19.42 3.898-26.501 11 l-48.761 48.761c-7.081 7.081-10.981 16.494-10.981 26.505c0 10 3.9 19.4 11 26.498l16.252 16.3 c7.075 7.1 16.5 11 26.5 10.981c10.011 0 19.424-3.9 26.506-10.981l27.478-27.478c0.884-0.884 1.127-2.223 0.611-3.361 c-0.489-1.077-1.561-1.76-2.731-1.76c-0.067 0-0.133 0.002-0.201 0.007c-0.967 0.065-1.947 0.097-2.914 0.1 c-7.632 0-15.138-2.029-21.705-5.868c-0.472-0.276-0.994-0.41-1.513-0.41c-0.776 0-1.544 0.301-2.122 0.879l-17.398 17.4 c-1.602 1.602-3.734 2.485-6.004 2.485c-2.271 0-4.405-0.882-6.007-2.485l-16.253-16.253c-1.604-1.6-2.487-3.729-2.489-5.996 c-0.001-2.272 0.882-4.408 2.489-6.015L85.868 85.869z"/> 
           <path d="M183.388 32.86l-16.253-16.253C160.059 9.5 150.6 5.6 140.6 5.63c-10.011 0-19.424 3.898-26.505 11 l-27.479 27.49c-0.884 0.884-1.127 2.224-0.61 3.362c0.489 1.1 1.6 1.8 2.7 1.759c0.067 0 0.135-0.002 0.203-0.007 c0.976-0.066 1.965-0.099 2.941-0.099c7.647 0 15.1 2 21.7 5.865c0.474 0.3 1 0.4 1.5 0.4 c0.776 0 1.544-0.301 2.122-0.879l17.41-17.41c1.599-1.6 3.732-2.48 6.005-2.48s4.406 0.9 6 2.48l16.253 16.3 c3.308 3.3 3.3 8.704-0.001 12.018l-48.759 48.759c-1.599 1.603-3.728 2.485-5.996 2.485c-2.273 0-4.41-0.883-6.018-2.487 L85.868 97.879c-0.563-0.562-1.326-0.879-2.121-0.879s-1.559 0.316-2.121 0.879l-16.253 16.253c-1.172 1.172-1.172 3.1 0 4.2 l16.253 16.253c7.081 7.1 16.5 11 26.5 10.981c10.008 0 19.42-3.9 26.501-10.981l48.76-48.76 c7.081-7.078 10.981-16.491 10.981-26.504C194.369 49.4 190.5 39.9 183.4 32.86z"/> 
          </g> 
         </svg> 
        </div> 
        <a href="http://<?php the_field('url'); ?>" target="_blank"><?php the_field('url'); ?></a> 
       </div> 
      <?php endif; ?> 
     </div> 
     <div id="project-right-content"> 

      <?php if(have_rows('slides')): ?> 

       <div id="slider"> 

        <!-- Slider Setup --> 
        <?php if(have_rows('slides')): 
         $slideNumber = 0; 
         while (have_rows('slides')) : the_row(); 
         $slideNumber++; 
        ?> 

         <input type="radio" name="slider" id="slide<?php echo $slideNumber; ?>"> 

        <?php endwhile;endif; ?> 

        <!-- Slide --> 
        <?php if(have_rows('slides')): ?> 
         <div id="slides"> 
          <div id="overflow"> 
           <div class="inner"> 

            <?php if(have_rows('slides')): 
            while (have_rows('slides')) : the_row(); 

             $slideImage = get_sub_field('slide_image'); 
            ?> 

            <article> 
             <img src="<?php echo $slideImage; ?>" alt="<?php the_title(); ?>"> 
            </article> 

            <?php endwhile;endif; ?> 

           </div><!-- #inner --> 
          </div><!-- #overflow --> 
         </div><!-- #slides --> 

        <?php endif; ?> 

        <!-- Controls --> 
        <?php if(have_rows('slides')): 
         $slideNumber = 0; 
        ?> 
         <div id="active"> 

          <?php while (have_rows('slides')) : the_row(); 
           $slideNumber++; 
          ?> 
           <label for="slide<?php echo $slideNumber; ?>"></label> 
          <?php endwhile; ?> 

         </div><!-- #active --> 
        <?php endif; ?> 
       </div><!-- #slider --> 
      <?php endif; ?> 
     </div><!-- #project-right-content --> 
    </div><!-- .post-container --> 

    <?php 
    endwhile; 
    wp_die(); 
} 

답변

0

아약스 호출이 제대로 작동하지 않으면 응답으로 "0"을 얻어야합니다. 그게 아니야? 콘솔에 오류가 있습니까?

내가 말한 문제는 라인 data: {'action': 'load-content', post_id: post_id }으로 시작한다고합니다. 액션은 load-content이라는 함수를 호출하지만 함수 이름에 빼기를 사용할 수 없기 때문에 수행되지 않습니다. 그 위에 'add_action'에 PHP 함수 이름에 빼기가 있습니다.

나는 다음이 작동하지 않을 수 있습니다 말을 믿지 :

function my_load_ajax_content() { 

    $args = array(
     'p' => $_POST['post_id'], 
     'post_type' => 'projects' 
     ); 

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

    <?php get_single_template() ?> 

    <?php 
    endwhile; 
    wp_die(); 
} 

add_action ('wp_ajax_nopriv_my_load_ajax_content', 'my_load_ajax_content'); // when the user is logged in 
add_action ('wp_ajax_my_load_ajax_content', 'my_load_ajax_content'); // when the user is not logged in 

$.ajax({ 
    type: 'POST', 
    url: ajaxURL, 
    context: this, 
    data: {'action': 'my_load_ajax_content', post_id: post_id }, 
    success: function(response) { 
     $('#project-container').html(response); 
     return false; 
    } 
}); 

는 PHP 오류를 찾을 수 있도록 functions.php의 시작 부분에 define('WP_DEBUG',true)을 추가하고 오류를 모니터 콘솔 . jquery가 실행 중인지 확인하려면 console.log(whatever)을 추가하십시오.

+0

나는 더 분명해야했다. 기본적으로, 나는 현재 작동 (나는 위의 게시물에 그것을 업로드) 기능을 가지고 있지만 대신'single.php' 템플릿으로 코드를 이동하고 대신 호출하려고합니다. – J82

+0

흠, 좋아. 테마 폴더에 'projects-single.php'라는 파일을 만든 다음 코드를 추가하십시오. 귀하의 게시물에 '프로젝트'유형이있을 때마다 Wordpress는이 파일을 자동으로 찾습니다. 맞춤 게시물 유형을 표시하려면 http://codex.wordpress.org/Post_Types를 참조하십시오. 희망이 좀 도와주세요. –

+0

그것이 내가 붙어있는 부분이다. 위의 첫 번째 코드 블록에서' '호출은 아무 것도하지 않습니다. 출력이 없습니다. 다른 단일 템플릿 (예 :'project-single.php')을 호출 할 때 문제가되지 않는 것 같습니다. 'single.php' 호출로도 동작하지 않아야합니까? – J82

0

워드 프레스 아약스 요청에 사용되는 당신이 보여준 코드입니다. 즉, 이들 함수를 functions.php에 작성해야합니다. 그런 다음 ajax를 반환하고 DOM을 업데이트하려는 경우 page.php/single.php에 자바 스크립트 코드를 작성해야합니다.

Google에서 wordpress ajax tutorial을 검색하고이 간단한 tutorial을 확인할 수 있습니다.

+0

함수가 이미'functions' 파일에 있습니다. 또한 Ajax 호출이 있으며이를 사용하여 게시물을 업데이트했습니다. 나는 그 함수에서 어떻게 하나의 템플릿을 얻을 수 있는지 궁금 할 뿐이다. – J82

관련 문제