2016-08-11 8 views
0

저는 Wordpress에 비교적 익숙합니다. 따라서 이것이 최선의 방법이 아니거나 가능하지 않을 수도 있습니다. 그 말로는, 나는 더 나은 해결책에 열려 있습니다.Wordpress 더 많은 버튼을 통해 글을 올리십시오.

나는 최신 12 개의 게시물을 표시 할 홈페이지가 있습니다. 이 날 이후 13 ~ 16의 세부 사항을로드

<?php query_posts('showposts=4&offset=12'); ?> 
<?php while (have_posts()): the_post(); ?> 
    //some html tags 
<?php endwhile; ?> 

: 나는 다음 4

로드됩니다 클릭하면 내가 현재 가지고하는 것은 이것이다하는 버튼을 만들고 싶어.

나는 이것을 함수에 넣을 수있는 곳을 찾고 있는데, 오프셋을 위해 일종의 카운터를 사용할 수있는 jquery click() 이벤트를 통해 호출 할 수 있습니다. something like

var offSetCounter = 12; 

$(".btnLoadMore").click(function(){ 
    //php function call 
    loadmore(offSetCounter); 
    offSetCounter += 4; 
}); 

그래서 다음 번에 버튼을 클릭하면 offSetCounter가 16이됩니다.

미리 감사드립니다.

PHP :

add_action('wp_ajax_nopriv_show_next_posts', 'show_next_posts'); 
function show_next_posts() { 
    $offset = $_POST['offset']; 
    $the_query = new new WP_Query(array('posts_per_page' => 4, 'offset' => $offset)); 



    while ($the_query->have_posts()) { 
     $the_query->the_post(); 
     echo '<div class="post_container">' . get_the_title() . '</div>'; 
    } 
    wp_reset_postdata(); 
    wp_die(); 
    return; 
} 

add_action('wp_head','add_ajaxurl'); 
function add_ajaxurl() { 
    $ajaxurl = admin_url('admin-ajax.php'); 
    echo '<script type="text/javascript"> 
     var ajaxurl ='.$ajaxurl.'; 
    </script>'; 

} 

JS :

jQuery('.btnLoadMore').click(function(){ 
    var offset = jQuery('.post_container').length(); 
    var data = { 
     'action': 'show_next_posts', 
     'offset': offset 
    }; 

    jQuery.post(ajaxurl, data, function(response) { 
     console.log(response); 
    }); 
}); 
+0

loadmore (offSetCounter가) 무엇을합니까 – stweb

+0

첫 번째 블록의 코드를 호출하는 사용자 지정 PHP 함수가 될 것으로 기대하고 있습니다. – Radizzt

+0

js/JQuery 코드에서 PHP에만 아약스 요청을 실행할 수 있지만 PHP 자체는 시작할 수 없습니다. – stweb

답변

0

이 시도 워드 프레스에서 실행하려면?
관련 문제